agentleFS
gurinderugurinderu/craft/skills/rust-web/SKILL.md

rust-web

Skill1 starsChanged 40 days agoMITView on GitHub

Web services in Rust with axum, tokio, tower, and sqlx — routing, extractors, shared state, error responses, database access, middleware, and graceful shutdown. Use when building an HTTP API/server, wiring axum handlers, sharing state, returning typed errors, or connecting a database. Triggers: axum, extractor, State, IntoResponse, tower, tower-http, sqlx, CORS.

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Rust Web (axum)

The de-facto async web stack — axum (routing/handlers) on tokio (runtime), tower (middleware), sqlx (database). The structural decisions belong to rust-architecture: a web server is just an inbound adapter over your domain. This skill is the axum-specific mechanics. Deep dives in the sub-files.

When to Use

  • Building an HTTP API or server in Rust
  • Wiring axum routes, extractors, handlers, shared state
  • Returning domain errors as HTTP responses
  • Connecting a database and adding middleware

The stack

[dependencies]
axum = "0.8"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "signal"] }
tower = "0.5"
tower-http = { version = "0.6", features = ["trace", "cors", "timeout", "compression-full"] }
sqlx = { version = "0.9", features = ["runtime-tokio", "postgres", "macros", "migrate"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Request lifecycle

TCP → tower middleware (trace, cors, timeout) → router matches path
    → extractors run (State, Path, Query, Json) → handler (async fn)
    → handler calls the domain service → Result<impl IntoResponse, ApiError>
    → response serialized back out

The handler is a translator, not a place for business logic: extract → call the domain → map the result to a response. Logic lives in the domain Service (→ rust-architecture).

Hello, server

use axum::{routing::get, Router};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let app = Router::new().route("/health", get(|| async { "ok" }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
    axum::serve(listener, app).await?;
    Ok(())
}

Decision points

QuestionAnswerWhere
Pass shared state (db pool, config) to handlersState<T> extractor + with_statehandlers.md
Return an error as a proper HTTP responseApiError: IntoResponsehandlers.md
Talk to a databasesqlx pool as state, queries in an outbound adapterdata-and-middleware.md
Add logging / CORS / timeoutstower-http layersdata-and-middleware.md
Shut down cleanlyaxum::serve(...).with_graceful_shutdown(...)data-and-middleware.md
Authenticate / authorize requestsargon2 + JWT + an auth extractor (401/403)auth.md
Generate OpenAPI / Swagger docsutoipa (ToSchema + #[utoipa::path])handlers.md

axum 0.8 note

Path parameters use brace syntax: /users/{id} (the old :id form was removed in 0.8). A route like /users/:id silently won't match — use {id} and extract with Path.

Boundaries

  • How to structure the app (domain core, ports, this server as an inbound adapter) → rust-architecture. This skill assumes that shape and fills in axum.
  • Domain errors → HTTP mapping is IntoResponse here, but the error design (typed variants, Unknown/defect) → rust-errors.
  • Arc shared state, Send futures, blocking-in-async → rust-concurrency.
  • Handler/integration testing (tower's oneshot, testcontainers) → rust-testing and the testing note in handlers.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.