This post shows a simple example of a REST API with Rust, Actix-web, and PostgreSQL and its requirements.
Rust Actix-web PostgreSQL Requirements
These are the items used for these posts and the subsequent ones.
- Windows 10
- Please see How to install Rust in Windows 10
- Docker for Windows
- Install Docker for Windows. These posts use Docker to create a PostgreSQL container.
- docker-compose.yml for PostgreSQL
- Please see docker-compose.yml for PostgreSQL. We will use the same username and password for the codes.
- IntelliJ IDEA (optional)
- Install the IntelliJ Rust plugin and create a new project.
- Rust 1.37.0
- Install it via rustup-init.exe.
Moreover, we will need a database table in our PostgreSQL. We will use a single table called persons, and we can create it using the following DDL. To run the SQL DDL, we need an SQL editor (e.g., pgAdmin) that will allow us to connect to PostgreSQL and perform database changes.
1 2 3 4 5 6 7 | create table persons ( person_id serial not null, person_name varchar(100) ); alter table persons owner to turreta; |
Rust Update Cargo.toml
The three crates we will use for our Rust Actix-web PostgreSQL project are postgres, actix-web, and serde.
1 2 3 4 5 6 7 8 9 10 11 12 | [package] name = "actix-web-demo-2" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] postgres = "0.15.2" actix-web = "1.0.7" serde = { version = "1.0.101", features = ["derive"] } |
Here are some details about these Rust crates. Please take note of the specific versions we use in this example.
- Crate postgres
- We need postgres for our Rust codes to access our PostgreSQL database, which runs in a Docker container.
- Crate actix-web
- Actix-web is a framework for creating web applications with Rust. We can think of this framework as something similar to Spring-web from Spring and Java or Laravel for PHP.
- Crate serde
- Serde is for the deserialization and serialization of HTTP requests and HTTP responses.
What’s Next For Our Rust Actix-web PostgreSQL
Next, we will talk about our Rust Actix-web PostgreSQL project structure overview.