Skip to main content

datapress_core/
server.rs

1//! Shared actix-web bootstrap. Both backends call [`serve`] from their
2//! own thin `serve(cfg)` entry point.
3
4use std::future::Future;
5use std::pin::Pin;
6use std::sync::Arc;
7use std::time::Duration;
8
9use actix_web::{App, HttpServer, middleware, web};
10
11use crate::backend::Backend;
12use crate::config::AppConfig;
13use crate::handlers;
14use crate::timeout::Timeout;
15
16/// How the running server is asked to begin a graceful shutdown.
17enum Shutdown {
18    /// Install `SIGINT`/`SIGTERM` (or `Ctrl+C`) handlers and stop when one
19    /// arrives. Used by the standalone binaries, which own the process and
20    /// its signal disposition.
21    Signals,
22    /// Stop when the given future resolves. Used when DataPress is embedded
23    /// (e.g. the Python extension), where the *host* owns signal handling
24    /// and drives shutdown by completing this future. No OS signal handlers
25    /// are installed, so we never fight the host's handlers.
26    External(Pin<Box<dyn Future<Output = ()> + Send>>),
27}
28
29/// Bind the HTTP server, register the generic handler set against
30/// `backend`, and run until the process receives `SIGINT` or `SIGTERM`.
31///
32/// Shutdown is **graceful**: on signal the listening socket is closed,
33/// existing connections get up to `cfg.server.shutdown_timeout_secs`
34/// seconds to drain in-flight requests, then workers are stopped.
35///
36/// `label` is the human-readable backend name used in the startup log
37/// line (e.g. `"DuckDB"`, `"DataFusion"`).
38pub async fn serve(cfg: AppConfig, backend: Arc<dyn Backend>, label: &str) -> std::io::Result<()> {
39    run_server(cfg, backend, label, Shutdown::Signals).await
40}
41
42/// Like [`serve`], but driven to a graceful stop by `shutdown` instead of
43/// OS signals.
44///
45/// Intended for embedding DataPress inside another runtime (the Python
46/// extension's `DataPress.run()`), where installing process-global signal
47/// handlers would race the host's own. The caller resolves `shutdown` —
48/// for example when its asyncio task is cancelled by `Ctrl+C` — and the
49/// server then drains in-flight requests within
50/// `cfg.server.shutdown_timeout_secs` and returns.
51pub async fn serve_with_shutdown(
52    cfg: AppConfig,
53    backend: Arc<dyn Backend>,
54    label: &str,
55    shutdown: impl Future<Output = ()> + Send + 'static,
56) -> std::io::Result<()> {
57    run_server(cfg, backend, label, Shutdown::External(Box::pin(shutdown))).await
58}
59
60async fn run_server(
61    cfg: AppConfig,
62    backend: Arc<dyn Backend>,
63    label: &str,
64    shutdown: Shutdown,
65) -> std::io::Result<()> {
66    let addr = (cfg.server.listen, cfg.server.port);
67    let workers = cfg.server.workers;
68    let prefix = cfg.server.prefix.clone();
69    let compress = cfg.server.compress;
70    let max_body = cfg.server.max_body_bytes;
71    let max_page_size = cfg.server.max_page_size;
72    let timeout_ms = cfg.server.request_timeout_ms;
73    let shutdown_secs = cfg.server.shutdown_timeout_secs;
74    let docs_cfg = cfg.docs.clone();
75    let swagger_cfg = cfg.swagger.clone();
76    let metrics_cfg = cfg.metrics.clone();
77    let explorer_cfg = cfg.explorer.clone();
78
79    // Warn (but don't fail) when the operator asked for docs in TOML but
80    // this binary was built without the cargo feature that embeds them.
81    #[cfg(not(feature = "docs"))]
82    if docs_cfg.enabled {
83        log::warn!(
84            "[docs] enabled = true in config, but this binary was built \
85             without --features docs; skipping docs site"
86        );
87    }
88    #[cfg(not(feature = "swagger"))]
89    if swagger_cfg.enabled {
90        log::warn!(
91            "[swagger] enabled = true in config, but this binary was built \
92             without --features swagger; skipping Swagger UI"
93        );
94    }
95    #[cfg(not(feature = "auth"))]
96    if cfg.auth.enabled {
97        log::warn!(
98            "[auth] enabled = true in config, but this binary was built \
99             without --features auth; skipping OIDC enforcement"
100        );
101    }
102    #[cfg(not(feature = "metrics"))]
103    if metrics_cfg.enabled {
104        log::warn!(
105            "[metrics] enabled = true in config, but this binary was built \
106             without --features metrics; skipping Prometheus endpoint"
107        );
108    }
109    #[cfg(not(feature = "explorer"))]
110    if explorer_cfg.enabled {
111        log::warn!(
112            "[explorer] enabled = true in config, but this binary was built \
113             without --features explorer; skipping explorer UI"
114        );
115    }
116
117    // Boot the JWKS cache (and validate config) before binding the
118    // listener. With `start_degraded = true` this only warns on an
119    // unreachable IdP; with `false` it propagates the error and the
120    // process exits non-zero.
121    #[cfg(feature = "auth")]
122    let auth_state = if cfg.auth.enabled {
123        let jwks = crate::auth::JwksCache::boot(&cfg.auth)
124            .await
125            .map_err(|e| std::io::Error::other(format!("auth bootstrap failed: {e}")))?;
126        log::info!(
127            "[auth] OIDC enforcement enabled (issuer = {}, audience = {}, read_scopes = {:?}, reload_scopes = {:?})",
128            cfg.auth.issuer,
129            if cfg.auth.audience.is_empty() {
130                "<none>"
131            } else {
132                cfg.auth.audience.as_str()
133            },
134            cfg.auth.read_scopes,
135            cfg.auth.reload_scopes,
136        );
137        Some(crate::auth::AuthState {
138            cfg: Arc::new(cfg.auth.clone()),
139            jwks,
140        })
141    } else {
142        None
143    };
144
145    log::info!(
146        "Listening on http://{}:{}{} ({} backend, {} workers, compression {}, max-body {} bytes, max-page-size {}, timeout {}, shutdown grace {}s)",
147        cfg.server.listen,
148        cfg.server.port,
149        if prefix.is_empty() {
150            "".into()
151        } else {
152            format!("{prefix}/")
153        },
154        label,
155        workers
156            .map(|w| w.to_string())
157            .unwrap_or_else(|| "auto".into()),
158        if compress { "on" } else { "off" },
159        max_body,
160        max_page_size,
161        if timeout_ms == 0 {
162            "off".into()
163        } else {
164            format!("{timeout_ms} ms")
165        },
166        shutdown_secs,
167    );
168
169    log_routes(&prefix, backend.as_ref());
170
171    #[cfg(feature = "docs")]
172    if docs_cfg.enabled {
173        log::info!("  {} (mkdocs site):", docs_cfg.path);
174        log::info!("    GET    {}/", docs_cfg.path);
175        log::info!("    GET    {}/{{path}}", docs_cfg.path);
176    }
177
178    #[cfg(feature = "swagger")]
179    if swagger_cfg.enabled {
180        log::info!("  {} (swagger UI):", swagger_cfg.path);
181        log::info!("    GET    {}/", swagger_cfg.path);
182        log::info!("    GET    {}/openapi.json", swagger_cfg.path);
183    }
184
185    #[cfg(feature = "explorer")]
186    if explorer_cfg.enabled {
187        log::info!("  {} (explorer UI):", explorer_cfg.path);
188        log::info!("    GET    {}/", explorer_cfg.path);
189        log::info!("    GET    {}/datasets/{{name}}", explorer_cfg.path);
190    }
191
192    // Resolve the Swagger UI's OIDC login endpoints once, before binding.
193    // We emit an explicit `oauth2` authorizationCode flow in the spec (see
194    // `swagger::ResolvedOAuth2`); discovering the authorize/token URLs here
195    // keeps the operator-facing config to just an `issuer`. On failure we
196    // log and serve the docs *without* a login button rather than shipping
197    // an empty Authorize dialog.
198    #[cfg(feature = "swagger")]
199    let swagger_oauth2 = if swagger_cfg.enabled {
200        match swagger_cfg.oauth2.as_ref() {
201            Some(o) => match crate::swagger::resolve_oauth2(o).await {
202                Ok(resolved) => Some(resolved),
203                Err(e) => {
204                    log::warn!(
205                        "[swagger.oauth2] OIDC discovery for issuer {} failed ({e}); \
206                         serving docs without the Authorize button",
207                        o.issuer
208                    );
209                    None
210                }
211            },
212            None => None,
213        }
214    } else {
215        None
216    };
217
218    // Build the Prometheus middleware once, outside the worker closure, so
219    // every worker shares a single registry (counts aggregate correctly).
220    // Constructed whenever the feature is compiled; the runtime `enabled`
221    // flag gates whether it is actually wrapped (and the endpoint served).
222    #[cfg(feature = "metrics")]
223    let prometheus = {
224        use actix_web_prom::PrometheusMetricsBuilder;
225        PrometheusMetricsBuilder::new("datapress")
226            .endpoint(metrics_cfg.path.as_str())
227            .build()
228            .map_err(|e| std::io::Error::other(format!("metrics init failed: {e}")))?
229    };
230    #[cfg(feature = "metrics")]
231    let metrics_enabled = metrics_cfg.enabled;
232
233    #[cfg(feature = "metrics")]
234    if metrics_cfg.enabled {
235        log::info!("  {} (prometheus metrics):", metrics_cfg.path);
236        log::info!("    GET    {}", metrics_cfg.path);
237    }
238
239    let build_info = web::Data::new(handlers::BuildInfo::new(
240        // `&'static str` so it fits BuildInfo's compile-time fields.
241        // The match keeps this generic enough for future backends.
242        match label {
243            "DuckDB" => "DuckDB",
244            "DataFusion" => "DataFusion",
245            _ => "unknown",
246        },
247    ));
248
249    // One Parquet export cache shared across all workers (it wraps an Arc),
250    // so a dataset is encoded at most once and every worker serves the same
251    // bytes for the ranged requests a Parquet reader makes.
252    let parquet_cache = web::Data::new(handlers::ParquetCache::default());
253
254    // One shared explorer state across all workers (it wraps an Arc backend).
255    // Built once here; each worker clones the `web::Data` handle.
256    #[cfg(feature = "explorer")]
257    let explorer_state = if explorer_cfg.enabled {
258        Some(web::Data::new(crate::explorer::ExplorerState {
259            backend: backend.clone(),
260            datasets: cfg.datasets.clone(),
261            explorer_base: explorer_cfg.path.clone(),
262            api_base: format!("{prefix}/api/v1"),
263            backend_label: label.to_string(),
264        }))
265    } else {
266        None
267    };
268
269    let mut server = HttpServer::new(move || {
270        let backend = backend.clone();
271        let prefix = prefix.clone();
272        let json_cfg = web::JsonConfig::default().limit(max_body);
273        let pay_cfg = web::PayloadConfig::default().limit(max_body);
274        let query_limits = handlers::QueryLimits { max_page_size };
275        let timeout = Timeout::new(Duration::from_millis(timeout_ms.max(1)));
276        #[cfg(feature = "docs")]
277        let docs_cfg = docs_cfg.clone();
278        #[cfg(feature = "explorer")]
279        let explorer_state = explorer_state.clone();
280        #[cfg(feature = "swagger")]
281        let swagger_cfg = swagger_cfg.clone();
282        #[cfg(feature = "swagger")]
283        let swagger_oauth2 = swagger_oauth2.clone();
284        #[cfg(feature = "auth")]
285        let auth_state = auth_state.clone();
286        #[cfg(feature = "metrics")]
287        let prometheus = prometheus.clone();
288        let app = App::new()
289            .app_data(web::Data::new(backend))
290            .app_data(build_info.clone())
291            .app_data(web::Data::new(query_limits))
292            .app_data(parquet_cache.clone())
293            .app_data(json_cfg)
294            .app_data(pay_cfg)
295            .wrap(middleware::Condition::new(timeout_ms > 0, timeout))
296            .wrap(middleware::Condition::new(
297                compress,
298                middleware::Compress::default(),
299            ))
300            .wrap(middleware::Logger::new("%a \"%r\" %s %b bytes %Dms"));
301        // Auth middleware wraps everything below — including the docs +
302        // swagger services and the prefix scope. Health/version probes
303        // are registered above and remain unauthenticated by design so
304        // load balancers can keep checking liveness. When auth is
305        // disabled the middleware is a pass-through.
306        #[cfg(feature = "auth")]
307        let app = match auth_state.clone() {
308            Some(state) => app
309                .app_data(web::Data::new(state.cfg.clone()))
310                .wrap(crate::auth::Auth::new(state)),
311            None => app.wrap(crate::auth::Auth::disabled()),
312        };
313        // Prometheus middleware sits OUTERMOST (added last → runs first) so
314        // it observes every request — including those auth rejects — and so
315        // the `/metrics` scrape it serves bypasses the auth layer entirely.
316        // `Condition` makes it a pass-through (and suppresses the endpoint)
317        // when `[metrics].enabled = false`.
318        #[cfg(feature = "metrics")]
319        let app = app.wrap(middleware::Condition::new(metrics_enabled, prometheus));
320        let app = app
321            .service(handlers::healthz)
322            .service(handlers::readyz)
323            .service(handlers::version);
324        // Docs + swagger are registered BEFORE the `web::scope(prefix)`
325        // catch-all below. An empty `prefix` (the default) becomes
326        // `web::scope("")` which matches every path and 404s any miss
327        // *inside* the scope — so services registered after it become
328        // unreachable. Keeping these at the top of the dispatch chain
329        // sidesteps that.
330        #[cfg(feature = "docs")]
331        let app = if docs_cfg.enabled {
332            app.configure(|c| crate::docs::configure(&docs_cfg.path, c))
333        } else {
334            app
335        };
336        #[cfg(feature = "swagger")]
337        let app = if swagger_cfg.enabled {
338            app.configure(|c| {
339                crate::swagger::configure(&swagger_cfg.path, swagger_oauth2.as_ref(), c)
340            })
341        } else {
342            app
343        };
344        // Explorer UI — registered (like docs/swagger) BEFORE the
345        // `web::scope(prefix)` catch-all so an empty prefix can't shadow it.
346        #[cfg(feature = "explorer")]
347        let app = match explorer_state {
348            Some(state) => app.configure(|c| crate::explorer::configure(state, c)),
349            None => app,
350        };
351        app.service(
352            web::scope(prefix.as_str())
353                .service(handlers::health)
354                // Canonical, versioned API.
355                .service(web::scope("/api/v1").configure(handlers::v1::configure))
356                // Legacy un-versioned alias. Kept around so older
357                // clients (and the historical `/api/datasets/...`
358                // URLs in docs / scripts) keep working. New code
359                // should prefer `/api/v1/...`.
360                .service(web::scope("/api").configure(handlers::v1::configure)),
361        )
362    });
363    if let Some(w) = workers {
364        server = server.workers(w);
365    }
366
367    // Disable actix's built-in signal handling so we can log which signal
368    // triggered shutdown, then drive the same `ServerHandle::stop(true)`
369    // path it would have used internally.
370    let running = server
371        .bind(addr)?
372        .shutdown_timeout(shutdown_secs)
373        .disable_signals()
374        .run();
375    let handle = running.handle();
376    tokio::spawn(shutdown_listener(handle, shutdown_secs, shutdown));
377
378    running.await
379}
380
381/// Wait for the configured shutdown trigger (OS signal or an external
382/// future), log it, then ask the actix server handle to stop gracefully.
383async fn shutdown_listener(
384    handle: actix_web::dev::ServerHandle,
385    grace_secs: u64,
386    shutdown: Shutdown,
387) {
388    match shutdown {
389        Shutdown::Signals => {
390            let which = wait_for_signal().await;
391            log::info!(
392                "Received {which}, shutting down gracefully (up to {grace_secs}s for in-flight requests)..."
393            );
394        }
395        Shutdown::External(fut) => {
396            fut.await;
397            log::info!(
398                "Shutdown requested by host, draining in-flight requests (up to {grace_secs}s)..."
399            );
400        }
401    }
402    handle.stop(true).await;
403    log::info!("Shutdown complete.");
404}
405
406#[cfg(unix)]
407async fn wait_for_signal() -> &'static str {
408    use tokio::signal::unix::{SignalKind, signal};
409    // `expect` is OK here: failing to install a signal handler at startup
410    // is a misconfigured runtime, not a recoverable condition.
411    let mut sigterm = signal(SignalKind::terminate()).expect("install SIGTERM handler");
412    let mut sigint = signal(SignalKind::interrupt()).expect("install SIGINT handler");
413    tokio::select! {
414        _ = sigterm.recv() => "SIGTERM",
415        _ = sigint.recv()  => "SIGINT",
416    }
417}
418
419#[cfg(not(unix))]
420async fn wait_for_signal() -> &'static str {
421    // Windows / other: only Ctrl+C is portably available through tokio.
422    let _ = tokio::signal::ctrl_c().await;
423    "Ctrl+C"
424}
425
426/// Pretty-print the route table at startup. Two sections:
427///   - general routes (health, probes)
428///   - per-dataset routes for every mounted API version (canonical
429///     `/api/v1/...` + the legacy un-versioned `/api/...` alias).
430fn log_routes(prefix: &str, backend: &dyn Backend) {
431    // Column widths chosen to fit the longest method + a comfortable
432    // path column. Names are inlined into the per-dataset paths.
433    const METHOD_W: usize = 6;
434
435    let p = prefix; // already validated to start with '/' or be empty
436
437    log::info!("Routes:");
438    log::info!("  general:");
439    for (method, path) in [
440        ("GET", "/healthz".to_string()),
441        ("GET", "/readyz".to_string()),
442        ("GET", "/version".to_string()),
443        ("GET", format!("{p}/health")),
444    ] {
445        log::info!("    {:<width$} {}", method, path, width = METHOD_W);
446    }
447
448    // Each API version is mounted under its own scope; we currently
449    // also expose v1 under the un-versioned `/api` for back-compat.
450    let mounts: &[(&str, &[(&str, &str)])] = &[
451        ("/api/v1", handlers::v1::ROUTES),
452        ("/api", handlers::v1::ROUTES), // legacy alias
453    ];
454
455    let names = backend.names();
456    for (mount, routes) in mounts {
457        log::info!("  {p}{mount}:");
458        // Top-level (non-dataset-scoped) routes for this version.
459        for (method, suffix) in *routes {
460            if !suffix.contains("{name}") {
461                log::info!(
462                    "    {:<width$} {p}{mount}{suffix}",
463                    method,
464                    width = METHOD_W,
465                );
466            }
467        }
468        if names.is_empty() {
469            log::info!("    (no datasets registered)");
470            continue;
471        }
472        for name in &names {
473            for (method, suffix) in *routes {
474                if let Some(rest) = suffix.strip_prefix("/datasets/{name}") {
475                    log::info!(
476                        "    {:<width$} {p}{mount}/datasets/{name}{rest}",
477                        method,
478                        width = METHOD_W,
479                    );
480                }
481            }
482        }
483    }
484}