Building The TCP Server
Install necessary dependencies
-
Switch to the
SeaORM-TODO-App/TODO-Server
directory to build the TCP server$ cd TODO-Server
-
Ensure you have installed Rust programming language https://www.rust-lang.org/tools/install
-
Ensure you have
sea-orm-cli
installed https://crates.io/crates/sea-orm-cli -
async-std
will be used as the async library$ cargo add async-std --features attributes
This adds async-std
src/Cargo.toml
file[package] name = "sea-orm-todo" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + async-std = { version = "1.10.0", features = ["attributes"] }
-
Add
anyhow
crate for error handling$ cargo add anyhow
An entry in the
Cargo.toml
file is added[package] name = "todo-client" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + anyhow = "1.0.53" async-std = { version = "1.10.0", features = ["attributes"] }
-
Add sea-orm with the features to enable sql drivers for PostgreSQL backend
$ cargo add sea-orm --no-default-features --features "runtime-async-std-rustls sqlx-postgres macros"
This adds sea-orm to
src/Cargo.toml
[package] name = "sea-orm-todo" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] anyhow = "1.0.53" async-std = { version = "1.10.0", features = ["attributes"] } + sea-orm = { version = "0.6.0", features = [ + "runtime-async-std-rustls", + "sqlx-postgres", + "macros", + ], default-features = false }
-
Login to Postgres database and create a new user and database
$ sudo -u postgres psql postgres
Create a new user in the PostgreSQL prompt.
postgres=# CREATE ROLE webmaster LOGIN PASSWORD 'master_char';
Create the
fruits_market
database and assign it to thewebmaster
userpostgres=# CREATE DATABASE fruits_market WITH OWNER = webmaster;
-
Create a
.env
file in the workspace directoryThe file structure should look
SeaORM-TODO-App |-- Cargo.toml |-- TODO-Client |-- src |-- Cargo.toml + |-- .env |-- TODO-Server
Then add the PostgreSQL configuration with the new user
webmaster
and databasefruits_market
we created earlierFile:
SeaORM-TODO-App/TODO-Server/.env
+ DATABASE_URL=postgres://webmaster:master_char@localhost/fruits_market
Change the main function to async function using async-std
- fn main() { - println!("Hello, world!"); - } + #[async_std::main] + async fn main() -> anyhow::Result<()> { + Ok(()) + }
Next, we will create all the required tables and their relationships