131 lines
3.2 KiB
Markdown
Raw Permalink 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
2022-08-06 12:48:21 +08:00
### FORMAT ON EVERY COMMIT
```
cargo fmt
```
2022-08-04 19:02:33 +08:00
### 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)
2022-08-06 12:48:21 +08:00
For example, we want a user table storing username and password. An Identity, a migration and the migration trait need to be defined:
2022-08-05 08:04:32 +08:00
```rust
2022-08-06 12:48:21 +08:00
// m20220804_000001_create_user_table.rs
2022-08-05 08:04:32 +08:00
#[derive(Iden)]
pub enum User { // Identifiers for User table
Table, // Table Identifier
Id, // Id Column Identifier
2022-08-06 12:48:21 +08:00
Username, // Name Column Identifier
2022-08-05 08:04:32 +08:00
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
2022-08-06 12:48:21 +08:00
.col(ColumnDef::new(User::Username).string().unique_key().not_null())
2022-08-05 08:04:32 +08:00
.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
2022-08-06 12:48:21 +08:00
```rust
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20220804_000001_create_user_table::Migration),
]
}
}
```
2022-08-05 08:04:32 +08:00
4. Run migrator
2022-08-06 12:48:21 +08:00
```
DATABASE_URL=sqlite://data.db cargo run -- migrate refresh
```
2022-08-05 08:04:32 +08:00
2022-08-06 12:48:21 +08:00
5. Generate entities
2022-08-05 08:04:32 +08:00
2022-08-06 12:48:21 +08:00
```
DATABASE_URL=sqlite://data.db sea-orm-cli generate entity --with-serde both -o ./src/web/model/
```
2022-08-04 19:02:33 +08:00
## Roadmap
- [x] Web
- [x] ORM
- [x] Packaging
- [ ] React