Skip to main content

Crate rust_web_server

Crate rust_web_server 

Source
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 http2 feature (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: validate rws.config.toml without starting the server — the counterpart of nginx -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 the New trait (a constructor used across the crate, much like Default).
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 JSON null, with FromStr (failing with ParseNullError), Display, Clone, Debug, PartialEq and New.
otel
OpenTelemetry-compatible distributed tracing.
pagination
Pagination result types (Page<T>, CursorPage<T>) and an RFC 8288 Link header 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: Request and 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_target gives 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.