108 lines
2.6 KiB
Markdown
Raw Normal View History

2022-08-04 19:02:33 +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](https://actix.rs): Async web framework for rust
- [SeaORM](https://www.sea-ql.org/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](src)
- [main.rs](src/main.rs): entrypoint
- [completion.rs](src/completion.rs): shell completion
- [migrations](src/migrations): migration scripts
- [web](src/web/): web apis
- [urls.rs](src/web/urls.rs): routes
- [model](src/web/model/): models generated by sea-orm-cli
- [view.rs](src/web/view.rs): url handlers
- [controller.rs](src/web/controller.rs): logics
- [README](README.md)
- [build](build): build tools
- [scripts](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](build/Dockerfile) and [script](scripts/build-container.sh) to build container image.
## Development
### Define a new model
2022-08-05 08:04:32 +08:00
1. Install sea-orm-cli first
```
cargo install sea-orm-cli
```
2. Define migration in [migrations](src/migrations)
```rust
#[derive(Iden)]
pub enum User { // Identifiers for User table
Table, // Table Identifier
Id, // Id Column Identifier
Name, // 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::Name).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
}
}
```
3. Add migration into migrator
> TODO
4. Run migrator
> TODO
5. Generate models
2022-08-04 19:02:33 +08:00
> TODO
## Roadmap
- [x] Web
- [x] ORM
- [x] Packaging
- [ ] React