rust-web-server 18.29.0

Single-binary web server and reverse proxy: automatic HTTPS (Let's Encrypt), HTTP/1.1, HTTP/2 and HTTP/3, load balancing, auth and rate limiting, configured in one TOML file. Also a Rust library: routing, middleware, MCP server, ORM, background jobs. No hyper, no Tower; HTTP/2 and HTTP/3 framing via h2 and quinn.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/// The `mimalloc` feature: the binary allocates with mimalloc (v3) — less
/// CPU per request than glibc's allocator (most of what's left of the
/// per-request cost in user space is allocation), more so on musl. Its
/// defaults hold freed memory for a second; purging at once
/// (`MIMALLOC_PURGE_DELAY=0`) uses less memory under large TLS downloads but
/// re-faults pages: 70 faults per 1 MiB request, up to 50% more CPU.
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use rust_web_server::app::App;
use rust_web_server::blocklist::BlocklistLayer;
use rust_web_server::cache::CacheLayer;
use rust_web_server::core::New;
use rust_web_server::maintenance::MaintenanceLayer;
use rust_web_server::metrics::SERVER_READY;
use rust_web_server::request::Request;
use rust_web_server::request_log::LogLayer;
use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
use rust_web_server::router::PathParams;
use rust_web_server::routes;
use rust_web_server::server::{ConnectionInfo, Server};
use std::sync::atomic::Ordering;

// ── shared state ──────────────────────────────────────────────────────────────

#[derive(Clone)]
struct AppState {
    version: &'static str,
}

// ── HTTP route handlers ───────────────────────────────────────────────────────

fn get_version(_req: &Request, _p: &PathParams, _c: &ConnectionInfo, state: &AppState) -> Response {
    let mut r = Response::new();
    r.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
    r.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
    r.content_range_list = vec![rust_web_server::range::Range::get_content_range(
        format!(r#"{{"version":"{}"}}"#, state.version).into_bytes(),
        "application/json".to_string(),
    )];
    r
}

fn echo_post(req: &Request, _p: &PathParams, _c: &ConnectionInfo, _s: &AppState) -> Response {
    let body = req.body.to_vec();
    let mut r = Response::new();
    r.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
    r.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
    r.content_range_list = vec![rust_web_server::range::Range::get_content_range(
        body,
        "application/json".to_string(),
    )];
    r
}

// ── app builder ───────────────────────────────────────────────────────────────
//
// Dispatch chain (outermost → innermost):
//
//  McpServer                 — POST /mcp  →  JSON-RPC (tools/list, tools/call …)
//    └─ AppWithState<S>      — custom routes registered below via routes!
//         └─ App (built-in)  — IndexController, HealthController (/healthz),
//                              ReadyController (/readyz), MetricsController,
//                              StaticResourceController, NotFoundController (404)
//
// Nothing below needs to change to keep the built-in controllers active;
// they run automatically for any request that neither McpServer nor
// AppWithState match.

fn build_app() -> impl rust_web_server::application::Application + Send + Clone + 'static {
    // 1. HTTP routes with shared state.
    let http = routes! {
        App::with_state(AppState { version: env!("CARGO_PKG_VERSION") }),
        GET  "/api/version" => get_version,
        POST "/api/echo"    => echo_post,
    };

    // Snapshot registered routes before mcp() consumes `http`.
    let registered_routes: Vec<(String, String)> = http.route_entries()
        .into_iter()
        .map(|r| (r.method, r.pattern))
        .collect();

    // 2. Shared cache — a clone kept for the MCP tools' stats and purge.
    //    Both share the same Arc<Mutex<CacheStore>> underneath.
    let cache = CacheLayer::memory(500).ttl(60);
    let tools_cache = cache.clone();
    rust_web_server::admin::ops::register_cache(cache.clone());
    rust_web_server::admin::ops::register_routes(registered_routes.clone());

    // 3. Around the routes: maintenance mode and the response cache.
    //    Maintenance stays inside MCP: the `maintenance` tool is how an
    //    operator switches it off again.
    let app = http.wrap(MaintenanceLayer).wrap(cache);

    // 4. MCP — only with a token (#44): its tools change the running server
    //    (config reload, blocklist, maintenance). Without MCP_TOKEN, /mcp
    //    isn't mounted at all. Browser origins must be listed in
    //    MCP_ALLOWED_ORIGINS (comma-separated).
    let token = std::env::var("MCP_TOKEN").unwrap_or_default();
    let served = if token.trim().is_empty() {
        println!("MCP tools not mounted: set MCP_TOKEN to enable /mcp.");
        Served::Plain(app)
    } else {
        let origins: Vec<String> = std::env::var("MCP_ALLOWED_ORIGINS")
            .unwrap_or_default()
            .split(',')
            .map(|o| o.trim().to_string())
            .filter(|o| !o.is_empty())
            .collect();
        let mcp = app
            .mcp("rws", env!("CARGO_PKG_VERSION"))
            .require_bearer(token.trim())
            .allowed_origins(origins);
        // Changes made through MCP tools land in the admin audit log (18.22.0).
        let audited = rust_web_server::admin::access::McpAuditLayer::new(rust_web_server::dev_mcp::OPS_WRITE_TOOLS);
        // The operations tools (rust_web_server::dev_mcp, since 18.28.0).
        let mcp = rust_web_server::dev_mcp::ops_tools(mcp, registered_routes, Some(tools_cache));
        Served::Mcp(rust_web_server::middleware::WithMiddleware::new(mcp).wrap(audited))
    };

    // 5. Outermost — for /mcp as well: tracing (when configured), access
    //    log, IP blocklist, global rate limit.
    traced(rust_web_server::middleware::WithMiddleware::new(served))
        .wrap(LogLayer)
        .wrap(BlocklistLayer)
        .wrap(GlobalRateLimit::from_env())
}

/// Whether requests are traced: `OTEL_EXPORTER_OTLP_ENDPOINT` (or the
/// traces one) set and `OTEL_TRACES_EXPORTER` not `none` — see [`telemetry`].
static TRACING: std::sync::OnceLock<bool> = std::sync::OnceLock::new();

/// Start OpenTelemetry export as the environment asks: metrics
/// (`http.server.request.duration`) and traces, each to its OTLP endpoint.
/// Nothing is exported, and no layer added, without an endpoint.
fn telemetry() {
    if rust_web_server::otel::metrics::start_from_env() {
        println!("OTLP metrics export on.");
    }
    if rust_web_server::otel::logs::start_from_env() {
        println!("OTLP log export on (access-log records).");
    }
    let tracing = rust_web_server::otel::traces_configured();
    if tracing {
        rust_web_server::otel::setup_from_env();
        println!("OTLP trace export on.");
    }
    let _ = TRACING.set(tracing);
}

/// `app` with `OtelLayer` outermost when tracing is on.
fn traced<A: rust_web_server::application::Application>(
    app: rust_web_server::middleware::WithMiddleware<A>,
) -> rust_web_server::middleware::WithMiddleware<A> {
    if TRACING.get().copied().unwrap_or(false) {
        app.wrap(rust_web_server::otel::OtelLayer)
    } else {
        app
    }
}

/// Either the MCP server (with the app behind it) or the app alone.
#[derive(Clone)]
enum Served<M, A> {
    Mcp(M),
    Plain(A),
}

impl<M: rust_web_server::application::Application, A: rust_web_server::application::Application>
    rust_web_server::application::Application for Served<M, A>
{
    fn execute(&self, request: &Request, connection: &ConnectionInfo) -> Result<Response, String> {
        match self {
            Served::Mcp(m) => m.execute(request, connection),
            Served::Plain(a) => a.execute(request, connection),
        }
    }

    fn runs_inline(&self, request: &Request, connection: &ConnectionInfo) -> bool {
        match self {
            Served::Mcp(m) => m.runs_inline(request, connection),
            Served::Plain(a) => a.runs_inline(request, connection),
        }
    }

    #[cfg(feature = "http2")]
    fn execute_async<'a>(
        &'a self,
        request: &'a Request,
        connection: &'a ConnectionInfo,
    ) -> Option<rust_web_server::application::AsyncResponse<'a>> {
        match self {
            Served::Mcp(m) => m.execute_async(request, connection),
            Served::Plain(a) => a.execute_async(request, connection),
        }
    }
}

// ── main ──────────────────────────────────────────────────────────────────────

/// The `[rate_limit]` / `RWS_CONFIG_RATE_LIMIT_*` per-client-IP limit,
/// enforced only when configured (decided once at startup; enabling it later
/// needs a restart, changing the numbers is hot-reloadable).
struct GlobalRateLimit(bool);

impl GlobalRateLimit {
    fn from_env() -> Self {
        GlobalRateLimit(rust_web_server::rate_limit::is_configured())
    }
}

impl rust_web_server::middleware::Middleware for GlobalRateLimit {
    /// Headers only: a streamed body passes through untouched.
    fn accepts_streamed_body(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
        true
    }

    fn handle(
        &self,
        request: &Request,
        connection: &ConnectionInfo,
        next: &dyn rust_web_server::application::Application,
    ) -> Result<Response, String> {
        if self.0 {
            rust_web_server::middleware::RateLimitLayer.handle(request, connection, next)
        } else {
            next.execute(request, connection)
        }
    }

    fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
        true // an in-memory counter
    }

    fn is_passthrough(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
        !self.0 // not configured: does nothing
    }
}

/// Automatic HTTPS: when `RWS_CONFIG_ACME_DOMAINS` is set, obtain (or reuse)
/// a certificate before the TLS listener starts, then keep it renewed.
#[cfg(feature = "http2")]
async fn start_acme(listener: Option<&std::net::TcpListener>) {
    #[cfg(feature = "acme")]
    {
        use rust_web_server::acme::{AcmeConfig, AcmeManager};
        if let Some(cfg) = AcmeConfig::from_env() {
            cfg.install_as_tls_certificate();
            // TLS-ALPN-01 before the server runs: answer on its own socket.
            if rust_web_server::acme::tls_alpn::enabled() {
                if let Some(socket) = listener.and_then(|l| l.try_clone().ok()) {
                    rust_web_server::acme::tls_alpn::set_startup_listener(socket);
                }
            }
            let mgr = AcmeManager::new(cfg.clone());
            mgr.provision_until_ready().await;
            tokio::spawn(mgr.run_renewal_loop());
            if rust_web_server::acme::on_demand::configured() {
                if let Err(e) = rust_web_server::acme::on_demand::start(&cfg) {
                    eprintln!("[ACME] {e}");
                }
            }
        }
    }
    #[cfg(not(feature = "acme"))]
    {
        let _ = listener;
        let domains = std::env::var(rust_web_server::entry_point::Config::RWS_CONFIG_ACME_DOMAINS)
            .unwrap_or_default();
        if !domains.trim().is_empty() {
            eprintln!(
                "[ACME] RWS_CONFIG_ACME_DOMAINS is set, but this rws binary was built without the \
                 `acme` feature — no certificate will be requested. Use a release binary or \
                 `cargo install rust-web-server --features acme`."
            );
        }
    }
}

/// `rws --version` / `rws --help`: print and exit before binding any socket.
/// `rws --check[=<file>]`: validate the configuration, exit 0 if it has no
/// errors, 1 otherwise.
fn exit_if_informational_flag() {
    let args: Vec<String> = std::env::args().collect();
    // `rws trust` / `rws untrust`: the local CA in the system trust store.
    #[cfg(feature = "http2")]
    if let Some(code) = rust_web_server::tls::internal::trust_command(&args) {
        std::process::exit(code);
    }
    // `rws fleet keygen` / `rws fleet sign`: the fleet config's keys.
    #[cfg(feature = "http2")]
    if let Some(code) = rust_web_server::fleet::command(&args) {
        std::process::exit(code);
    }
    // `rws ctl …` / `rwsctl …`: a client of the admin API, not a server.
    #[cfg(not(target_arch = "wasm32"))]
    if let Some(ctl_args) = rust_web_server::ctl::requested(&args) {
        std::process::exit(rust_web_server::ctl::run(ctl_args, &mut std::io::stdout(), &mut std::io::stderr()));
    }
    // `rws gen …`: code generators for the project in the working directory.
    #[cfg(not(target_arch = "wasm32"))]
    if let Some(gen_args) = rust_web_server::generator::requested(&args) {
        std::process::exit(rust_web_server::generator::run(gen_args, &mut std::io::stdout(), &mut std::io::stderr()));
    }
    if let Some(output) = rust_web_server::entry_point::command_line_args::informational_flag_output(&args) {
        print!("{output}");
        std::process::exit(0);
    }
    if let Some(path) = rust_web_server::config_check::check_target_from_args(&args) {
        let report = rust_web_server::config_check::check_file(&path);
        if rust_web_server::config_check::wants_json_output(&args) {
            println!("{}", report.render_json(&path));
        } else {
            print!("{}", report.render(&path));
        }
        std::process::exit(if report.is_ok() { 0 } else { 1 });
    }
}

#[cfg(not(feature = "http2"))]
fn main() {
    exit_if_informational_flag();
    let (listener, pool) = Server::setup().expect("server setup failed");
    rust_web_server::log::Log::reopen_access_log_on_sigusr1();
    rust_web_server::config_watch::start();
    rust_web_server::admin::start_from_env();
    rust_web_server::cluster::start_from_env();
    #[cfg(feature = "http2")]
    rust_web_server::fleet::start_from_env();
    telemetry();
    if rust_web_server::proxy_config::ProxyConfig::is_proxy_mode() {
        let (proxy_app, _handles) = rust_web_server::proxy_config::build_from_file();
        let proxy_app = traced(rust_web_server::middleware::WithMiddleware::new(proxy_app)).wrap(MaintenanceLayer).wrap(BlocklistLayer).wrap(GlobalRateLimit::from_env());
        SERVER_READY.store(true, Ordering::SeqCst);
        #[cfg(unix)]
        rust_web_server::systemd::ready("serving");
        Server::run(listener, pool, proxy_app);
    } else {
        SERVER_READY.store(true, Ordering::SeqCst);
        #[cfg(unix)]
        rust_web_server::systemd::ready("serving");
        Server::run(listener, pool, build_app());
    }
    rust_web_server::otel::shutdown();
    rust_web_server::traffic::save();
}

/// The server: everything `main` runs once the runtime is up.
#[cfg(all(feature = "http2", not(feature = "http3")))]
async fn serve() {
    rust_web_server::server::per_core::default_to_per_core();
    let (listener, pool) = Server::setup().expect("server setup failed");
    rust_web_server::log::Log::reopen_access_log_on_sigusr1();
    telemetry();
    #[cfg(feature = "gateway-api")]
    rust_web_server::gateway_api::start_from_env();

    rust_web_server::tls::internal::start_from_env();
    start_acme(Some(&listener)).await;
    // After ACME, not before: provisioning writes the certificate and key the
    // watcher watches, and its SIGHUP arrived before run_tls installs the
    // handler, so the signal's default action ended the process.
    rust_web_server::config_watch::start();
    rust_web_server::admin::start_from_env();
    rust_web_server::cluster::start_from_env();
    #[cfg(feature = "http2")]
    rust_web_server::fleet::start_from_env();

    if rust_web_server::proxy_config::ProxyConfig::is_proxy_mode() {
        let (proxy_app, _handles) = rust_web_server::proxy_config::build_from_file();
        let proxy_app = traced(rust_web_server::middleware::WithMiddleware::new(proxy_app)).wrap(MaintenanceLayer).wrap(BlocklistLayer).wrap(GlobalRateLimit::from_env());
        SERVER_READY.store(true, Ordering::SeqCst);
        #[cfg(unix)]
        rust_web_server::systemd::ready("serving");
        tokio::join!(
            Server::run_tls(listener, pool, proxy_app.clone()),
            Server::run_redirect(),
        );
    } else {
        SERVER_READY.store(true, Ordering::SeqCst);
        #[cfg(unix)]
        rust_web_server::systemd::ready("serving");
        tokio::join!(
            Server::run_tls(listener, pool, build_app()),
            Server::run_redirect(),
        );
    }
    let _ = tokio::task::spawn_blocking(rust_web_server::otel::shutdown).await;
    let _ = tokio::task::spawn_blocking(rust_web_server::traffic::save).await;
}

/// The server: everything `main` runs once the runtime is up.
#[cfg(feature = "http3")]
async fn serve() {
    rust_web_server::server::per_core::default_to_per_core();
    let (listener, pool) = Server::setup().expect("server setup failed");
    rust_web_server::log::Log::reopen_access_log_on_sigusr1();
    telemetry();
    #[cfg(feature = "gateway-api")]
    rust_web_server::gateway_api::start_from_env();

    rust_web_server::tls::internal::start_from_env();
    start_acme(Some(&listener)).await;
    // After ACME, not before: provisioning writes the certificate and key the
    // watcher watches, and its SIGHUP arrived before run_tls installs the
    // handler, so the signal's default action ended the process.
    rust_web_server::config_watch::start();
    rust_web_server::admin::start_from_env();
    rust_web_server::cluster::start_from_env();
    #[cfg(feature = "http2")]
    rust_web_server::fleet::start_from_env();

    if rust_web_server::proxy_config::ProxyConfig::is_proxy_mode() {
        let (proxy_app, _handles) = rust_web_server::proxy_config::build_from_file();
        let proxy_app = traced(rust_web_server::middleware::WithMiddleware::new(proxy_app)).wrap(MaintenanceLayer).wrap(BlocklistLayer).wrap(GlobalRateLimit::from_env());
        SERVER_READY.store(true, Ordering::SeqCst);
        #[cfg(unix)]
        rust_web_server::systemd::ready("serving");
        tokio::join!(
            Server::run_tls(listener, pool, proxy_app.clone()),
            Server::run_quic(proxy_app),
            Server::run_redirect(),
        );
    } else {
        SERVER_READY.store(true, Ordering::SeqCst);
        #[cfg(unix)]
        rust_web_server::systemd::ready("serving");
        // One app for both listeners (building it twice printed its
        // startup notes twice).
        let app = build_app();
        tokio::join!(
            Server::run_tls(listener, pool, app.clone()),
            Server::run_quic(app),
            Server::run_redirect(),
        );
    }
    // Last metrics and spans out before the process ends; blocking, so off
    // the runtime's workers.
    let _ = tokio::task::spawn_blocking(rust_web_server::otel::shutdown).await;
    let _ = tokio::task::spawn_blocking(rust_web_server::traffic::save).await;
}

/// A multi-threaded tokio runtime, as `#[tokio::main]` builds one.
#[cfg(feature = "http2")]
fn runtime() -> tokio::runtime::Runtime {
    tokio::runtime::Builder::new_multi_thread().enable_all().build().expect("tokio runtime")
}

#[cfg(feature = "http2")]
fn main() {
    exit_if_informational_flag();
    #[cfg(windows)]
    if let Some(directory) = windows_service_mode::requested() {
        windows_service_mode::run(directory);
        return;
    }
    runtime().block_on(serve());
}

/// `rws --windows-service=<directory>`: run under the Windows Service Control
/// Manager, serving `<directory>` (and reading its `rws.config.toml`). A
/// service starts in `System32` with no console, so the directory is
/// required and logs belong in a file (`access_log`). Stop and Shutdown
/// controls shut the server down gracefully, as SIGTERM does on Unix.
#[cfg(all(windows, feature = "http2"))]
mod windows_service_mode {
    use std::ffi::OsString;
    use std::time::Duration;
    use windows_service::service::{
        ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus, ServiceType,
    };
    use windows_service::service_control_handler::{self, ServiceControlHandlerResult};
    use windows_service::{define_windows_service, service_dispatcher};

    const FLAG: &str = "--windows-service=";

    /// The directory given with `--windows-service=`, if the flag is present.
    pub fn requested() -> Option<String> {
        std::env::args().find_map(|arg| arg.strip_prefix(FLAG).map(str::to_string))
    }

    define_windows_service!(ffi_service_main, service_main);

    pub fn run(directory: String) {
        if let Err(e) = std::env::set_current_dir(&directory) {
            eprintln!("--windows-service: can't serve {directory}: {e}");
            std::process::exit(1);
        }
        // The service's name comes from the SCM; for an own-process service
        // the name passed here isn't checked.
        if let Err(e) = service_dispatcher::start("rws", ffi_service_main) {
            eprintln!("--windows-service: not started by the Service Control Manager: {e}");
            std::process::exit(1);
        }
    }

    fn service_main(_arguments: Vec<OsString>) {
        let handler = |control| match control {
            ServiceControl::Stop | ServiceControl::Shutdown => {
                rust_web_server::server::request_shutdown();
                ServiceControlHandlerResult::NoError
            }
            ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
            _ => ServiceControlHandlerResult::NotImplemented,
        };
        let Ok(status) = service_control_handler::register("rws", handler) else { return };
        let report = |state, accepted| {
            let _ = status.set_service_status(ServiceStatus {
                service_type: ServiceType::OWN_PROCESS,
                current_state: state,
                controls_accepted: accepted,
                exit_code: ServiceExitCode::Win32(0),
                checkpoint: 0,
                wait_hint: Duration::from_secs(10),
                process_id: None,
            });
        };
        report(ServiceState::Running, ServiceControlAccept::STOP | ServiceControlAccept::SHUTDOWN);
        super::runtime().block_on(super::serve());
        report(ServiceState::Stopped, ServiceControlAccept::empty());
    }
}