This is a sample Rust microservice that can be deployed on Kubernetes.
This is a pet project for learning purposes that started from this tutorial.
Execute this demo app in minikube with:
minikube start --mount --mount-string=$PWD:/mnt/host --cpus=4
skaffold dev --trigger=manual --iterative-status-check
This project demonstrates how to create:
- a Rocket web app
- writing data to PostgreSQL & Redis
- execute DB migrations on deploy
- type safe SQL queries
- deploying to Kubernetes with Cloud Native Buildpacks
- different profiles (default, local, test etc.)
- externalized configuration
- rest input validation
- centralized error handling with
thiserror
andanyhow
- tracer that logs messages with a http request ID for easy correlation
- unit tests with mocks
- testing connected to a test db
- http testing
rust-auth.mp4
Skaffold will build the app, deploy it in the cluster, watch for code changes and cleanup on quit.
Use kubectl
to generate k8s yaml files (and customize them according your needs).
kubectl create blablabla --dry-run=client -o yaml > ops/k8s/file.yaml
Execute the app locally:
skaffold dev -m migrations --iterative-status-check --port-forward
APP_PROFILE=local cargo run
Execute the tests locally:
skaffold dev -m migrations --iterative-status-check --port-forward
DATABASE_URL=$(dasel -f App.toml -r toml test.db.url) sqlx database reset -y && cargo test
skaffold init -k ./ops/k8s/storage/*.yaml --skip-build
skaffold dev -m migrations --iterative-status-check --port-forward
Now you have the servers available for connection at localhost. Both dev & test databases get created and migrations executed.
sudo apt-get install libpq-dev
cargo install sqlx-cli --no-default-features --features native-tls,postgres
curl -sSLf "$(curl -sSLf https://api.github.com/repos/tomwright/dasel/releases/latest | grep browser_download_url | grep linux_amd64 | cut -d\" -f 4)" -L -o dasel && chmod +x dasel
mv ./dasel /usr/local/bin/dasel
You can use the sqlx cli to create migration files and prepare the sqlx-data.json
, and
dasel cli is useful to parse toml files. For example, to validate your SQL queries against your local DB and generate the json file, you can execute:
DATABASE_URL=$(dasel -f App.toml -r toml local.db.url) cargo sqlx prepare -- --lib