This is a simply REST server written in Rust with axum and tokio-postgres.
I'm using this to figure out a good structure for such a project and answer questions like:
- where to put what (controllers, app state, models, etc, etc.)
- how to handle migrations (and how to run them)
- how to handle configuration for different environments
The server requires a database to run. The connection string is read from the
.env
file and defaults to
postgresql://rust_rest:rust_rest@localhost/rust_rest
. The repo contains a
Docker setup for a PostgreSql database that will work with that connection
string. Run via:
docker compose up
You can also change the connection string and connect to a different (PostgreSql) database of course:
DATABASE_URL="postgresql://<user>:<password>@<host>/<database>"
Once the database is running, it needs to be migrated so that the required tables get created etc.:
cargo db migrate
cargo run
curl -X POST localhost:3000/task -H 'Authorization: Bearer secr3t!' -H 'Content-Type: application/json' -d '{"description": "do something"}'
curl localhost:3000/tasks/<id>
curl localhost:3000/tasks
Migrate the test database:
cargo db migrate -e test
Then run the tests:
cargo test