Rust replacement for the kbr-api Rails backend. Shares the same PostgreSQL schema during a migration transition period.
- Rust 1.95.0, edition 2024
actix-web4.x (HTTP framework)sqlx0.8.x (async PostgreSQL)jsonwebtoken(JWT auth)bcrypt(password hashing)tokio(async runtime)serde/serde_json(serialization)tracing(logging)
- All async tests must use
#[tokio::test(flavor = "current_thread")] std::env::{set_var, remove_var}areunsafein edition 2024 — wrap inunsafe {}- Avoid string literals for permission resources — use
PermissionResource/RESOURCES AppErroris the single error type; implementResponseErrorfor HTTP responsesAppStateholds shared state (PgPool); routes borrow viaweb::Data<AppState>
src/
main.rs — server bootstrap, routes, AppState
lib.rs — module re-exports
error.rs — AppError enum + ResponseError impl
bin/
kbr_migrate.rs — migration CLI (migrate, rollback, status, check)
auth/
jwt.rs — Claims, encode/decode, get_jwt_secret
roles.rs — Role enum
permissions.rs — PermissionResource, has_permission, is_admin, is_artist_or_above, is_customer_or_above
resources.rs — RESOURCES constant, ResourceNames
middleware.rs — CurrentUser extractor, FromRequest
db/
pool.rs — PgPool connection setup
migrate.rs — sqlx::migrate runner, health check
handlers/ — HTTP handlers (one file per resource)
models/ — DB models (sqlx types)
responses/ — response serializers
services/ — business logic
cargo test --lib— run all unit testscargo build— build binarycargo test— run all tests (including integration)cargo run --bin kbr-migrate -- migrate— run pending migrationscargo run --bin kbr-migrate -- check— schema health check
- Rails source:
/Users/ws/formasimplex/kbr-api - Rust patterns:
/Users/ws/formasimplex/tenant_daemon