axum from scratch
just because.
example up and running
running the example from the repo https://github.com/tokio-rs/axum?tab=readme-ov-file#usage-example
add deps
—
run it
curl it
watch it instead (re-run on file changes)
now start the server with cargo watch
-q
: quiet mode. reduces output verbosity.
-c
: clears the screen before each run.
-w src/
: watches the src/
directory for changes.
-x run
: executes cargo run
on detected changes.
add a database
docker-compose
we need a local db to connect to. use docker-compose.
add an .env
file with secrets for pg.
prefix env vars with the app name to avoid conflicts with the 1000s of other apps you’ve created and didn’t finish on your system ☠️
dont forget to add the .env file to your .gitignore
.gitignore could look like this:
create a docker-compose.yml file
i like to keep a Makefile in the root of my projects for common tasks
sqlx
use postgres and sqlx
let’s introduce AppState
to our app:
(see axum create example: https://docs.rs/axum/latest/axum/index.html#sharing-state-with-handlers)
clean up main.rs and add a database connection
integrating sea orm
let’s add sea orm to our project using the seo-orm axum example
update cargo.toml
migrations
install
add a set of migration commands to the makefile
user table migration
let’s create a generic user table corresponding to this:
update the migration file created on init
and run the migration
links