axum-restful
A restful framework based on axum
and sea-orm
. Inspired by django-rest-framework
.
The goal of the project is to build an enterprise-level production framework.
Features
- a Trait for the
struct
generated bysea-orm
to provide with GET, PUT, DELETE methods tls
supportprometheus
metrics and metrics servergraceful shutdown
supportswagger document
generate based onaide
Quick start
A full example is exists at axum-restful/examples/demo
.
First, you can create a new crate like cargo new axum-restful-demo
.
Build a database service
You should have a database service before. It is recommended to use postgresql
database.
you can use docker and docker compose to start a postgresql
create a compose.yaml
in the same directory as Cargo.toml
services:
postgres:
image: postgres:15-bullseye
container_name: demo-postgres
restart: always
volumes:
- demo-postgres:/var/lib/postgresql/data
ports:
- "127.0.0.1:5432:5432"
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
volumes:
demo-postgres:
a .env
file like
# config the base pg connect params
POSTGRES_DB=demo
POSTGRES_USER=demo-user
POSTGRES_PASSWORD=demo-password
# used by axum-restful framework to specific a database connection
DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}
finally, you can build a service with docker compose up -d
Write and migrate a migration
For more details, please refer to the sea-orm
documentation.
Install the sea-orm-cli
with cargo
$ cargo install sea-orm-cli
Configure dependencies and workspace in Cargo.toml
[]
= "demo"
= "0.1.0"
= "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[]
= [".", "migration"]
[]
= "0.13"
= "0.7"
= "0.5"
= "0.4"
= { = "./migration" }
= "1"
= { = "0.8", = ["chrono"] }
= { = "0.12", = ["macros", "sqlx-postgres", "runtime-tokio-rustls"] }
= { = "0.12", = ["sqlx-postgres", "runtime-tokio-rustls",] }
= { = "1.0", = ["derive"] }
= "1.0"
= { = "1", = ["full"] }
= "0.1"
= "0.3"
Setup the migration directory in ./migration
$ sea-orm-cli migrate init
project structure changed into
├── Cargo.lock
├── Cargo.toml
├── compose.yaml
├── migration
│ ├── Cargo.toml
│ ├── README.md
│ └── src
│ ├── lib.rs
│ ├── m20220101_000001_create_table.rs
│ └── main.rs
└── src
└── main.rs
edit the m20****_******_create_table.rs
file blow ./migration/src
use *;
;
/// Learn more at https://docs.rs/sea-query#iden
edit migration/Cargo.toml
to add dependencies
[]
...
= "0.5"
edit migration/src/main.rs
to specific a database connection an migrate
use *;
async
migrate the migration files
$ cd migration
$ cargo run
finally, you can see two tables named sql_migrations
and student
generated.
Generate entities
at the project root path
$ sea-orm-cli generate entity -o src/entities
will generate entities configure and code, now project structure changed into
├── Cargo.lock
├── Cargo.toml
├── compose.yaml
├── migration
│ ├── Cargo.toml
│ ├── README.md
│ └── src
│ ├── lib.rs
│ ├── m20220101_000001_create_table.rs
│ └── main.rs
└── src
├── entities
│ ├── mod.rs
│ ├── prelude.rs
│ └── student.rs
└── main.rs
edit the src/entities/student.rs
to add derive Default, Serialize, Deserialize
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
use JsonSchema;
use *;
use ;
edit src/main.rs
use JsonSchema;
use MigratorTrait;
use TcpListener;
use SwaggerGeneratorExt;
use ModelViewExt;
use crate student;
async
StudentView impl the ModelView<T>
, the T
is student::ActiveModel
that represent the student table configure
in the database, if will has full HTTP methods with GET, POST, PUT, DELETE.
you can see the server is listen at port 3000
Verify the service
Swagger
if you impl axum_restful::swagger::SwaggerGenerator
above, then you can visit http://127.0.0.1:3000/docs/swagger/
at your browser, you will see a swagger document is generated
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.