Expand description
§rust-web-server
An HTTP/1.1, HTTP/2, and HTTP/3 (QUIC) server, reverse proxy, and
application framework in one crate. HTTP/1.1 parsing, routing, middleware,
JSON, WebSocket, and SSE are implemented in-crate; HTTP/2 and HTTP/3
framing use the h2 and quinn/h3 crates, and TLS uses rustls.
§Use as a library
Add to Cargo.toml:
[dependencies]
rust-web-server = "17"§Quick start
use rust_web_server::prelude::*;
fn hello(_: &Request, _: &PathParams, _: &ConnectionInfo, _: &()) -> Response {
Response::text(STATUS_CODE_REASON_PHRASE.n200_ok, "Hello, world!")
}
fn main() {
let app = App::with_state(()).get("/hello", hello);
let (listener, pool) = Server::setup().unwrap();
Server::run(listener, pool, app);
}See DEVELOPER.md for the full building blocks reference and use case examples.
Modules§
- app
- application
- Application: the type a server runs for every request.
- async_
state - Async-capable state-aware application — requires the
http2feature (tokio). - blocklist
- Runtime IP blocklist middleware.
- body
- Request and response bodies.
- cache
- In-memory response cache middleware.
- canary
- Weighted canary / A-B traffic splitting middleware.
- circuit_
breaker - Circuit breaker state machine and retry middleware.
- client_
hint - compression
- config_
binding - Typed configuration binding — read environment variables into strongly-typed structs.
- config_
check rws --check: validaterws.config.tomlwithout starting the server — the counterpart ofnginx -t/caddy validate.- config_
reload - Hot configuration reload.
- config_
watch - Reload when the config file or a certificate changes (
watch = true, #10). - controller
- Controllers: one unit of request handling for
App. - cookie
- core
- Small shared utilities:
base64,sha256(SHA-256, HMAC-SHA256) and theNewtrait (a constructor used across the crate, much likeDefault). - cors
- di
- Lightweight type-keyed dependency injection container.
- entry_
point - error
- ext
- extract
- feature
- Runtime feature toggles.
- header
- HTTP header fields.
- http
- http_
client - Outbound HTTP/1.1 client.
- ingress
- Kubernetes Ingress watcher and router.
- ip_
filter - IP address allowlist and denylist middleware.
- json
- A dependency-free JSON parser and serializer.
- language
- log
- macros
- Declarative routing macro.
- maintenance
- Maintenance mode middleware.
- mcp
- Model Context Protocol (MCP) server — HTTP Streamable HTTP transport.
- metrics
- Server-wide and per-route Prometheus metrics.
- middleware
- Composable middleware pipeline.
- mime_
type - null
Null: a value representing JSONnull, withFromStr(failing withParseNullError),Display,Clone,Debug,PartialEqandNew.- otel
- OpenTelemetry-compatible distributed tracing.
- pagination
- Pagination result types (
Page<T>,CursorPage<T>) and an RFC 8288Linkheader builder. - prelude
- Convenience re-exports for writing handlers and running the server.
- proxy
- Reverse proxy middleware with round-robin load balancing.
- proxy_
config - Config-driven proxy application.
- range
- Byte ranges (RFC 9110 §14) and response bodies.
- rate_
limit - request
- HTTP requests:
Requestand its parser. - request_
id - Request ID / correlation ID middleware.
- request_
log - In-memory ring buffer of recent HTTP requests.
- response
- HTTP responses:
Response, its writers and the upstream-response parser. - rewrite
- Request and response rewriting middleware.
- router
- scheduler
- Background task scheduler — a
@Scheduled-style runner for fixed-rate, fixed-delay, and cron-expression tasks. - schema
- JSON Schema for Rust types, for MCP tool
inputSchema/outputSchema. - server
- The listeners and connection loops.
- server_
config - Typed server configuration.
- service_
discovery - Dynamic backend pool with pluggable discovery sources.
- session
- Server-side session management.
- sse
- Server-Sent Events (SSE).
- state
- Shared application state and state-aware routing.
- symbol
- tcp_
proxy - Layer-4 TCP proxy.
- test_
client - thread_
pool - timeout
- Per-route request timeouts.
- udp_
proxy - Layer-4 UDP proxy.
- url
- URL helpers: percent encoding (
URL::percent_encode/percent_decode), query strings (URL::build_query/parse_query) and whole URLs (URL::build/parse, a panic-free RFC 3986 parser).URL::parse_request_targetgives a request’s path and query without inventing a scheme and host. Percent encoding is what lets a query value carry a space (%20),=(%3D) or&(%26). Examples:src/url/example/. - validate
- virtual_
host - websocket
- WebSocket protocol support — RFC 6455.
- ws_
proxy - WebSocket reverse proxy.
Macros§
- routes
- Build a routing app from a declarative table.