Skip to main content

rust_web_server/
lib.rs

1//! # rust-web-server
2//!
3//! A static file web server and HTTP toolkit written in Rust.
4//! Supports HTTP/3 (QUIC), HTTP/2, and HTTP/1.1.
5//!
6//! ## Use as a library
7//!
8//! Add to `Cargo.toml`:
9//!
10//! ```toml
11//! [dependencies]
12//! rust-web-server = "17"
13//! ```
14//!
15//! ## Quick start: add a custom route
16//!
17//! ```rust,no_run
18//! use rust_web_server::controller::Controller;
19//! use rust_web_server::request::{METHOD, Request};
20//! use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
21//! use rust_web_server::range::Range;
22//! use rust_web_server::mime_type::MimeType;
23//! use rust_web_server::server::ConnectionInfo;
24//!
25//! pub struct PingController;
26//!
27//! impl Controller for PingController {
28//!     fn is_matching(request: &Request, _: &ConnectionInfo) -> bool {
29//!         request.method == METHOD.get && request.request_uri == "/ping"
30//!     }
31//!
32//!     fn process(_: &Request, mut response: Response, _: &ConnectionInfo) -> Response {
33//!         response.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
34//!         response.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
35//!         response.content_range_list = vec![
36//!             Range::get_content_range(b"pong".to_vec(), MimeType::TEXT_PLAIN.to_string())
37//!         ];
38//!         response
39//!     }
40//! }
41//! ```
42//!
43//! See [DEVELOPER.md](https://github.com/bohdaq/rust-web-server/blob/main/DEVELOPER.md)
44//! for the full building blocks reference and use case examples.
45
46// Allows `::rust_web_server::…` paths to resolve from within this crate's own
47// tests, which is required by proc-macro derive output that uses that prefix.
48extern crate self as rust_web_server;
49
50pub mod app;
51#[cfg(feature = "auth")]
52pub mod auth;
53
54#[cfg(feature = "macros")]
55pub use rws_macros::{delete, get, patch, post, put, route, Config, FromRequest, Validate};
56#[cfg(all(feature = "macros", any(feature = "model-sqlite", feature = "model-postgres", feature = "model-mysql")))]
57pub use rws_macros::Model;
58#[cfg(feature = "http2")]
59pub mod async_state;
60#[cfg(feature = "http2")]
61pub(crate) mod async_bridge;
62pub mod session;
63pub(crate) mod redis_protocol;
64pub mod sse;
65pub mod compression;
66pub mod cookie;
67pub mod error;
68pub mod extract;
69pub mod ip_filter;
70pub mod macros;
71pub mod blocklist;
72pub mod cache;
73pub mod config_reload;
74pub mod server_config;
75pub mod feature;
76pub mod maintenance;
77pub mod metrics;
78pub mod mcp;
79pub mod request_log;
80pub mod request_id;
81pub mod otel;
82#[cfg(feature = "acme")]
83pub mod acme;
84pub mod middleware;
85pub mod rate_limit;
86pub mod router;
87pub mod state;
88pub mod test_client;
89pub mod application;
90pub mod body;
91pub mod client_hint;
92pub mod controller;
93pub mod core;
94pub mod cors;
95pub mod entry_point;
96pub mod ext;
97pub mod header;
98pub mod http;
99pub mod json;
100pub mod language;
101pub mod log;
102pub mod mime_type;
103pub mod null;
104pub mod range;
105pub mod request;
106pub mod response;
107pub mod server;
108pub mod symbol;
109pub mod thread_pool;
110pub mod url;
111pub mod pagination;
112pub mod proxy;
113pub mod rewrite;
114pub mod scheduler;
115pub mod tcp_proxy;
116pub mod udp_proxy;
117pub mod ws_proxy;
118pub mod canary;
119pub mod circuit_breaker;
120pub mod service_discovery;
121pub mod config_binding;
122pub mod di;
123pub mod proxy_config;
124pub mod ingress;
125#[cfg(feature = "tera")]
126pub mod template;
127pub mod validate;
128pub mod virtual_host;
129#[cfg(any(feature = "model-sqlite", feature = "model-postgres", feature = "model-mysql"))]
130pub mod model;
131pub mod websocket;
132pub mod http_client;
133#[cfg(feature = "crypto")]
134pub mod crypto;
135#[cfg(feature = "csrf")]
136pub mod csrf;
137#[cfg(feature = "sso")]
138pub mod sso;
139#[cfg(feature = "mailer")]
140pub mod mailer;
141#[cfg(feature = "jobs")]
142pub mod jobs;
143#[cfg(any(feature = "storage-local", feature = "storage-s3", feature = "storage-azure"))]
144pub mod storage;
145#[cfg(feature = "openapi")]
146pub mod openapi;
147#[cfg(feature = "webhook")]
148pub mod webhook;
149pub mod timeout;
150pub mod prelude;
151
152#[cfg(feature = "http2")]
153#[doc(hidden)]
154pub mod tls;
155
156#[cfg(feature = "http2")]
157#[doc(hidden)]
158pub mod h2_handler;
159
160#[cfg(feature = "http3")]
161#[doc(hidden)]
162pub mod h3_handler;
163
164/// Shared infrastructure for tests that write process-wide environment variables.
165///
166/// Tests that call `override_environment_variables_from_config` or
167/// `CommandLineArgument::set_environment_variable` must hold this lock for
168/// their entire duration so they don't race with other tests reading the
169/// same variables.
170#[cfg(test)]
171pub mod test_env {
172    use std::sync::{Mutex, OnceLock};
173    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
174    pub fn lock() -> std::sync::MutexGuard<'static, ()> {
175        LOCK.get_or_init(|| Mutex::new(()))
176            .lock()
177            .unwrap_or_else(|e| e.into_inner())
178    }
179}