2022-08-23 16:17:22 +08:00
2022-08-04 19:06:18 +08:00
2022-08-04 19:06:18 +08:00
2022-08-23 16:17:22 +08:00
2022-08-04 19:06:18 +08:00
2022-08-04 17:43:33 +08:00
2022-08-23 16:17:22 +08:00
2022-08-23 16:17:22 +08:00
2022-08-06 12:48:21 +08:00

Rust Template Project for Development with Actix-Web and SeaORM

A lot of Thanks to these projects

This project template is made of:

  • Rust
  • actix-web: Async web framework for rust
  • SeaORM: async ORM for integrating a Rust code base with relational databases

And I won't reach this without the help of:

  • sqlx
  • askama
  • clap
  • clap_complete
  • serde
  • env_logger
  • log

Project strucutre

Start from here

Run the server

Simply use cargo to run the project

Package into container

The template provides a simple Dockerfile and script to build container image.

Development

FORMAT ON EVERY COMMIT

cargo fmt

Define a new model

  1. Install sea-orm-cli first
cargo install sea-orm-cli
  1. Define migration in migrations

For example, we want a user table storing username and password. An Identity, a migration and the migration trait need to be defined:

// m20220804_000001_create_user_table.rs

#[derive(Iden)]
pub enum User { // Identifiers for User table
    Table, // Table Identifier
    Id, // Id Column Identifier
    Username, // Name Column Identifier
    Password, // Password Column Identifier
}

impl MigrationName for Migration {
    fn name(&self) -> &str {
        "m20220804_000001_create_user_table" // A migration name `m$(date +%Y%m%d)_8digitno_desc`
    }
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: & sea_orm_migration::SchemaManager) ->  Result<(),sea_orm::DbErr> {
        manager.create_table(
            Table::create()
                .table(User::Table) // Table
                .col(ColumnDef::new(User::Id).integer().primary_key().auto_increment().not_null()) // And each col
                .col(ColumnDef::new(User::Username).string().unique_key().not_null())
                .col(ColumnDef::new(User::Password).string().not_null())
                .to_owned()
        ).await
    }

    async fn down(&self, manager: & sea_orm_migration::SchemaManager) ->  Result<(),sea_orm::DbErr> {
        manager.drop_table(Table::drop().table(User::Table).to_owned()).await
    }
}
  1. Add migration into migrator
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
    fn migrations() -> Vec<Box<dyn MigrationTrait>> {
        vec![
            Box::new(m20220804_000001_create_user_table::Migration),
        ]
    }
}
  1. Run migrator
DATABASE_URL=sqlite://data.db cargo run -- migrate refresh
  1. Generate entities
DATABASE_URL=sqlite://data.db sea-orm-cli generate entity --with-serde both -o ./src/web/model/

Roadmap

  • Web
  • ORM
  • Packaging
  • React
Description
No description provided
Readme 67 KiB
Languages
Rust 93.2%
Dockerfile 4.2%
Shell 2.6%