This is archived – life goes on at https://github.com/marcoow/pacesetter
This is a simple REST server written in Rust with axum and sqlx.
I'm using this to figure out a good structure for such a project and answer questions like:
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 and seeded so that the required tables get created and data gets inserted:
cargo db migrate
cargo db seed
The Docker setup already comes with pre-configured databases for development and testing but they can also be dropped, created, and reset (drop, re-create, migrate) via the command line if necessary (e.g. when switching between branches with different database schemes):
cargo db drop
cargo db create
cargo db reset
In order to generate a new migration, use the generate
command:
cargo generate migration <migration-name>
cargo run
The log level can be set via the RUST_LOG
env var (set one of trace
,
debug
, info
(default), warn
, error
), e.g.:
RUST_LOG=trace cargo run
curl
curl -X POST localhost:3000/tasks -H 'Authorization: 9974812642a36dbee625fa06b2463dbff832e17dcce3836dbb' -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
This project is developed by and © Mainmatter GmbH and contributors. It is released under the MIT License.