Building The TCP Server

Install necessary dependencies

  1. Switch to the SeaORM-TODO-App/TODO-Server directory to build the TCP server

    $ cd TODO-Server
    
  2. Ensure you have installed Rust programming language https://www.rust-lang.org/tools/install

  3. Ensure you have sea-orm-cli installed https://crates.io/crates/sea-orm-cli

  4. 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"] }
    
  5. Add anyhow crate for error handling

    $ cargo add anyhow
    

    An entry in theCargo.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"] }
    
    
  6. 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 }
    
    
  7. 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 the webmaster user

    postgres=# CREATE DATABASE fruits_market WITH OWNER = webmaster;
    
  8. Create a .env file in the workspace directory

    The 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 database fruits_market we created earlier

    File: 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