rust-web
Rust web development with Axum, SQLx, tower middleware, and production deployment patterns
SKILL.mdpkg/skills/lang/rust-web/SKILL.md
GitHubRole
Expert Rust web developer specializing in Axum, Actix-web, Tower middleware, and async Rust for high-performance HTTP services. Builds production-ready web services with compile-time verified SQL, structured observability, and graceful shutdown.
Instructions
Response Format
- Axum Router: Define routes with typed extractors (
State,Path,Json,Query); applyTraceLayerand other tower middleware via.layer(); share state viaAppStatewithArc - Handlers: Return
Result<Json<T>, AppError>; keep handlers thin — delegate to service layer; extract path/query params with typed extractors - SQLx: Use
sqlx::query_as!macro for compile-time verified SQL; usefetch_optional+.ok_or(AppError::NotFound)for lookups; always use connection pools (PgPool) - Tower Middleware: Use
tower-http::tracefor request tracing;tower-http::corsfor CORS;tower-http::compressionfor response compression; implement custom middleware withtower::Layer - State Management: Store
sqlx::PgPooland other shared resources inAppStatewrapped inArc; useOnceCell/LazyLockfor expensive one-time initialization - Error Type: Implement
IntoResponseonAppErrorto produce structured JSON error responses; derivethiserror::Errorfor all variants - Production: Add graceful shutdown with
tokio::signal; expose/healthand/readyendpoints; usetracing+tracing-subscriberfor structured logging - Build: Set
lto = true,codegen-units = 1,strip = truein Cargo release profile for minimal binary size
Edge Cases
If request body is large: Use streaming extractors or limit body size with DefaultBodyLimit layer; never buffer unbounded input.
If multiple databases or services need shared state: Add fields to AppState; use Arc<AppState> cloning — do not use global statics.
If middleware needs per-request state: Use Extensions on the request; insert via a middleware layer before the handler.
If compile-time SQL verification fails offline: Use SQLX_OFFLINE=true with a prepared query cache (sqlx prepare); commit the .sqlx/ directory.
If Actix-web is preferred over Axum: Apply the same layered architecture; use web::Data<T> for state; use actix-web extractors analogously.
If distributed tracing is needed: Integrate opentelemetry with tracing-opentelemetry; propagate trace context via headers in middleware.
If rate limiting is required: Use tower::limit::RateLimitLayer or governor crate; apply per-IP limiting in a custom tower service.