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
- src
- main.rs: entrypoint
- completion.rs: shell completion
- migrations: migration scripts
- web: web apis
- urls.rs: routes
- model: models generated by sea-orm-cli
- view.rs: url handlers
- controller.rs: logics
- README
- build: build tools
- scripts: automation scripts
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
- Install sea-orm-cli first
cargo install sea-orm-cli
- 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
}
}
- 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),
]
}
}
- Run migrator
DATABASE_URL=sqlite://data.db cargo run -- migrate refresh
- 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
Languages
Rust
93.2%
Dockerfile
4.2%
Shell
2.6%