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;
60pub mod session;
61pub mod sse;
62pub mod compression;
63pub mod cookie;
64pub mod error;
65pub mod extract;
66pub mod ip_filter;
67pub mod macros;
68pub mod blocklist;
69pub mod cache;
70pub mod config_reload;
71pub mod server_config;
72pub mod feature;
73pub mod maintenance;
74pub mod metrics;
75pub mod mcp;
76pub mod request_log;
77pub mod otel;
78#[cfg(feature = "acme")]
79pub mod acme;
80pub mod middleware;
81pub mod rate_limit;
82pub mod router;
83pub mod state;
84pub mod test_client;
85pub mod application;
86pub mod body;
87pub mod client_hint;
88pub mod controller;
89pub mod core;
90pub mod cors;
91pub mod entry_point;
92pub mod ext;
93pub mod header;
94pub mod http;
95pub mod json;
96pub mod language;
97pub mod log;
98pub mod mime_type;
99pub mod null;
100pub mod range;
101pub mod request;
102pub mod response;
103pub mod server;
104pub mod symbol;
105pub mod thread_pool;
106pub mod url;
107pub mod proxy;
108pub mod rewrite;
109pub mod scheduler;
110pub mod tcp_proxy;
111pub mod udp_proxy;
112pub mod ws_proxy;
113pub mod canary;
114pub mod circuit_breaker;
115pub mod service_discovery;
116pub mod config_binding;
117pub mod di;
118pub mod proxy_config;
119pub mod ingress;
120#[cfg(feature = "tera")]
121pub mod template;
122pub mod validate;
123pub mod virtual_host;
124#[cfg(any(feature = "model-sqlite", feature = "model-postgres", feature = "model-mysql"))]
125pub mod model;
126pub mod websocket;
127pub mod http_client;
128#[cfg(feature = "crypto")]
129pub mod crypto;
130#[cfg(feature = "csrf")]
131pub mod csrf;
132#[cfg(feature = "sso")]
133pub mod sso;
134#[cfg(feature = "mailer")]
135pub mod mailer;
136pub mod prelude;
137
138#[cfg(feature = "http2")]
139#[doc(hidden)]
140pub mod tls;
141
142#[cfg(feature = "http2")]
143#[doc(hidden)]
144pub mod h2_handler;
145
146#[cfg(feature = "http3")]
147#[doc(hidden)]
148pub mod h3_handler;
149
150/// Shared infrastructure for tests that write process-wide environment variables.
151///
152/// Tests that call `override_environment_variables_from_config` or
153/// `CommandLineArgument::set_environment_variable` must hold this lock for
154/// their entire duration so they don't race with other tests reading the
155/// same variables.
156#[cfg(test)]
157pub mod test_env {
158    use std::sync::{Mutex, OnceLock};
159    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
160    pub fn lock() -> std::sync::MutexGuard<'static, ()> {
161        LOCK.get_or_init(|| Mutex::new(()))
162            .lock()
163            .unwrap_or_else(|e| e.into_inner())
164    }
165}