Rocket
An async web framework for Rust that uses type-safe macros for routing, request guards, and fairings to catch invalid requests before your handlers run.
Repository Health
Technical Analysis
Rocket is an async web framework for Rust built around type safety and developer ergonomics. Routes are declared with attribute macros like #[get(”/<name>”)], and Rocket uses Rust’s type system to guard against common web vulnerabilities at compile time — malformed input simply fails to match a route rather than reaching handler code.
Beyond routing, Rocket ships with a request guard system for extracting and validating data, a fairing system for request/response middleware, built-in support for JSON, forms, cookies, and static files, and first-party contrib crates for structured logging, database connection pooling, and template rendering. The core library builds on Hyper and Tokio for its async HTTP stack, with optional TLS, mutual TLS, and experimental HTTP/3 support behind feature flags.
What You Get
- Attribute-macro routing (#[get], #[post], #[put], #[delete]) that binds path segments, query params, and request bodies directly to typed function arguments
- A request guard system for composable, reusable extraction and validation logic — authentication, database connections, cookies — expressed as ordinary function parameters
- Fairings — global middleware hooks for request/response lifecycle events, used internally for Rocket’s own logging and security-header shield
- Built-in JSON, msgpack, form, and static-file handling, plus managed state for sharing application data across all routes without global mutable statics
- Official contrib crates for async/sync database pooling (db_pools, sync_db_pools), templating (dyn_templates), and WebSockets (ws)
Common Use Cases
- Building typed JSON REST APIs where malformed requests are rejected before handler code runs
- Serving a full server-rendered web application via the dyn_templates contrib crate
- Adding mutual-TLS-authenticated internal services using the mtls feature
- Wrapping a database (Postgres, SQLite, etc.) behind async connection pooling via db_pools
- Prototyping a small HTTP API quickly using Rocket’s minimal, macro-driven boilerplate
Under The Hood
Architecture
Rocket is a Cargo workspace split into core/lib (the rocket crate — the runtime: Rocket struct, Route, Request, Response, Data, routing, fairings, request guards), core/codegen (a proc-macro crate implementing the #[get]/#[post]/#[launch]/derive macros that generate routing glue at compile time), and core/http (shared low-level HTTP types — Method, Status, headers, URI parsing — used by both lib and codegen). Contrib crates (contrib/db_pools, contrib/sync_db_pools, contrib/dyn_templates, contrib/ws) layer optional integrations on top of the core three via fairings and request guards, keeping the core dependency-light. Execution starts at the #[launch]-annotated fn, which returns a Rocket<Build> from rocket::build().mount(path, routes![…]); rocket.rs then orchestrates the ignite (config + fairing on-ignite hooks) and launch (binding the listener) phases, and each inbound request flows through router/matcher.rs to find a matching Route and then route/handler.rs, which invokes the handler after satisfying its request guards. Changing the core Route/Request abstractions would ripple through router/collider.rs (route collision detection), the codegen crate’s macro-generated FromParam/FromData impls, and every contrib crate depending on request guards — a modular but tightly macro-coupled design.
Tech Stack
The rocket crate targets Rust edition 2021 (rust-version 1.75) and builds its async HTTP stack on hyper 1.x and hyper-util (feature-gated http1/http2/server), driven by Tokio. Core dependencies include yansi for terminal coloring, time for date/time handling, memchr for parsing, ref-cast/ref-swap for zero-cost wrapper types, and cookie (with private/key-expansion features behind the secrets flag) for session cookies. Optional features pull in serde_json (json), rmp-serde (msgpack), uuid, rustls/tokio-rustls/rustls-pemfile (tls), x509-parser (mtls), and s2n-quic/s2n-quic-h3 for the experimental http3-preview feature. The codegen crate is a syn/quote-based proc-macro crate generating route/form/derive glue at compile time; contrib crates add sqlx/deadpool-based pooling and Handlebars/Tera templating as separate opt-in dependencies.
Code Quality The core/lib/src tree carries inline #[test] modules across many files (e.g. router/collider.rs, route/segment.rs) exercising route-matching edge cases, alongside a dedicated testbench crate and 21 runnable examples under examples/ that double as integration coverage via CI. Error handling favors typed enums and the Outcome (Success/Failure/Forward) pattern rather than panics, letting request guards fail gracefully and fall through to the next matching route. Naming is idiomatic Rust throughout (snake_case modules, CamelCase traits like FromRequest/FromData), and the workspace enforces a shared lint policy via [workspace.lints.rust]/[workspace.lints.clippy] in the root Cargo.toml. CI runs on every push. No standalone coverage tooling was found in the top-level config, but the combination of inline unit tests, a testbench crate, and example-driven integration coverage points to a disciplined test culture.
API Design Rocket’s defining choice is pushing request validation into the type system via attribute macros and the FromParam/FromData/FromRequest traits — a route like #[get(”/<age>”)] fn hello(age: u8) simply never executes if age fails to parse, eliminating a class of manual validation code common in other Rust web frameworks that compose extractors more explicitly. The request-guard model lets arbitrary preconditions (auth, DB connections, rate limits) be expressed as ordinary typed function parameters with Outcome-based fallthrough to the next matching route, rather than hard failure — a distinctive extensibility mechanism compared to tower-style middleware stacks. Getting started requires minimal boilerplate (a #[launch] fn and a handful of route attributes), though the breadth of feature flags and contrib crates adds surface area to learn as an application grows. This is an opinionated, ergonomics-first design within the Rust web space rather than a wholly novel general-purpose technique.
Used by 2 apps in this directory
Meilisearch
Search
Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.
Vaultwarden
Password Manager · Security
Unofficial Bitwarden-compatible server in Rust — run the full Bitwarden ecosystem on a Raspberry Pi using every official client you already have, without the multi-container overhead.