navree-http 1.0.2

Navree HttpModule: Navree's router and extractors, default Tower layers, JSON errors, controllers and route metadata.
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
//! [`HttpModule`], the [`HttpServer`] provider and the `serve`, `routes`,
//! `down` and `up` commands.

use std::net::SocketAddr;
use std::sync::Arc;

use navree_config::{Config, ModuleBuilderConfigExt};
use navree_kernel::{
    AppContext, BoxError, BoxFuture, CommandSpec, ConfiguredModule, Factory, Injectable,
    KernelError, Lifecycle, Module, ModuleBuilder, ProviderKey, Resolver,
};
use tokio::sync::watch;

use crate::body::BodyError;
use crate::extract::rejection;
use crate::maintenance::{MaintenanceInfo, MaintenanceMode};
use crate::{
    AppError, HttpOptions, ModuleBuilderHttpExt, RouteMeta, UrlSigner, build_router, route_table,
};

/// The HTTP module: the server, default layer stack, JSON errors,
/// controllers. Global; provides and exports [`HttpOptions`], [`HttpServer`]
/// and [`UrlSigner`]; registers the commands `serve`, `routes`, `down` and
/// `up`; and checks every route at boot (access declarations, duplicate
/// routes, missing authenticator or authorizer), before `serve` binds a port.
///
/// ```no_run
/// use navree_http::{HttpModule, HttpOptions};
/// use navree_kernel::Navree;
///
/// # fn main() -> Result<(), navree_kernel::BoxError> {
/// Navree::app()
///     .module(HttpModule::for_root(HttpOptions::default()))
///     .run()
/// # }
/// ```
///
/// Used plainly (`.module(HttpModule)`) or through
/// [`from_config`](HttpModule::from_config), it reads the `http`
/// configuration section, so the app must include `ConfigModule`.
#[derive(Clone, Copy, Debug, Default)]
pub struct HttpModule;

impl HttpModule {
    /// The module with explicit options (configure it once, in the app root).
    pub fn for_root(options: HttpOptions) -> impl Module {
        ConfiguredModule::<HttpModule, _>::for_root(options, register)
    }

    /// The module configured from the `http` section of `Config` at boot.
    pub fn from_config() -> impl Module {
        ConfiguredModule::<HttpModule, HttpOptions>::from_config(register)
    }
}

impl Module for HttpModule {
    fn register(&self, m: &mut ModuleBuilder) {
        register(m, None);
    }
}

fn register(m: &mut ModuleBuilder, options: Option<HttpOptions>) {
    m.global();
    match options {
        Some(options) => m.value(options),
        None => m.config::<HttpOptions>(),
    };
    m.exports::<HttpOptions>()
        .provider::<HttpServer>()
        .exports::<HttpServer>()
        .lifecycle::<HttpServer>()
        .factory(
            Factory::<UrlSigner>::new(|r: Resolver| async move {
                let options = r.get::<HttpOptions>().await?;
                let config = r.app().try_get::<Config>().await;
                Ok(Arc::new(url_signer(&options, config.as_deref())))
            })
            .deps(vec![ProviderKey::of::<HttpOptions>()]),
        )
        .exports::<UrlSigner>()
        .map_error::<rejection::JsonRejection>(|r| Some(r.to_app_error()))
        .map_error::<rejection::FormRejection>(|r| Some(r.to_app_error()))
        .map_error::<rejection::QueryRejection>(|r| Some(r.to_app_error()))
        .map_error::<rejection::PathRejection>(|r| Some(r.to_app_error()))
        .map_error::<rejection::ExtensionRejection>(|r| Some(r.to_app_error()))
        .map_error::<rejection::MultipartRejection>(|r| Some(r.to_app_error()))
        .map_error::<rejection::MultipartError>(|r| Some(r.to_app_error()))
        .map_error::<rejection::WebSocketRejection>(|r| Some(r.to_app_error()))
        .map_error::<BodyError>(|r| Some(AppError::rejection(r.status(), r.to_string())))
        .command(CommandSpec::new(
            "serve",
            "Start the HTTP server ([--addr HOST:PORT])",
            serve,
        ))
        .command(CommandSpec::new(
            "routes",
            "List every route with its access declaration ([--json])",
            routes,
        ))
        .command(CommandSpec::new(
            "down",
            "Put the app into maintenance mode ([--retry SECS] [--message TEXT])",
            down,
        ))
        .command(CommandSpec::new(
            "up",
            "Bring the app out of maintenance mode",
            up,
        ));
}

/// The signer for `http.signing_key`; else a key derived from the app key
/// (`app.key`, i.e. `APP_KEY`), so signed URLs survive restarts; else a
/// random per-process key.
fn url_signer(options: &HttpOptions, config: Option<&Config>) -> UrlSigner {
    let app_key = || config?.app_key().map(|key| key.expose().clone());
    if let Some(key) = options.signing_key.as_ref().map(|k| k.expose().as_str())
        && !key.is_empty()
    {
        return UrlSigner::new(key.as_bytes());
    }
    match app_key() {
        Some(app_key) => UrlSigner::derived_from_app_key(&app_key),
        None => {
            tracing::debug!(
                target: "navree::http",
                "neither http.signing_key nor app.key is set; signed URLs use a random key and stop working after a restart"
            );
            let mut key = Vec::with_capacity(32);
            key.extend_from_slice(uuid::Uuid::new_v4().as_bytes());
            key.extend_from_slice(uuid::Uuid::new_v4().as_bytes());
            UrlSigner::new(&key)
        }
    }
}

/// The HTTP server state shared by the commands and the pipeline: options,
/// maintenance mode and the address `serve` is bound to.
pub struct HttpServer {
    options: Arc<HttpOptions>,
    pub(crate) maintenance: Arc<MaintenanceMode>,
    bound: watch::Sender<Option<SocketAddr>>,
}

impl HttpServer {
    /// The HTTP options.
    pub fn options(&self) -> &HttpOptions {
        &self.options
    }

    /// The maintenance-mode state.
    pub fn maintenance(&self) -> &MaintenanceMode {
        &self.maintenance
    }

    /// The address `serve` listens on, while it runs.
    pub fn local_addr(&self) -> Option<SocketAddr> {
        *self.bound.borrow()
    }

    /// Waits until `serve` is listening and returns its address (useful with
    /// `addr = "127.0.0.1:0"`).
    pub async fn wait_bound(&self) -> SocketAddr {
        let mut receiver = self.bound.subscribe();
        loop {
            if let Some(addr) = *receiver.borrow_and_update() {
                return addr;
            }
            if receiver.changed().await.is_err() {
                std::future::pending::<()>().await;
            }
        }
    }

    fn set_bound(&self, addr: Option<SocketAddr>) {
        self.bound.send_replace(addr);
    }
}

impl std::fmt::Debug for HttpServer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HttpServer")
            .field("options", &self.options)
            .field("local_addr", &self.local_addr())
            .finish_non_exhaustive()
    }
}

impl Injectable for HttpServer {
    fn dependencies() -> Vec<ProviderKey> {
        vec![ProviderKey::of::<HttpOptions>()]
    }

    fn construct(r: Resolver) -> BoxFuture<'static, Result<Self, KernelError>> {
        Box::pin(async move {
            let options = r.get::<HttpOptions>().await?;
            if let Err(errors) = options.check() {
                return Err(KernelError::Construction {
                    provider: std::any::type_name::<HttpOptions>().to_owned(),
                    source: errors.join("; ").into(),
                });
            }
            Ok(Self {
                maintenance: Arc::new(MaintenanceMode::new(options.maintenance_file.clone())),
                options,
                bound: watch::Sender::new(None),
            })
        })
    }
}

/// Builds the router once at boot, so route mistakes fail the boot.
impl Lifecycle for HttpServer {
    fn on_bootstrap(&self, ctx: AppContext) -> BoxFuture<'_, Result<(), BoxError>> {
        Box::pin(async move { build_router(&ctx).await.map(drop) })
    }
}

/// The value of `--name VALUE` (or `--name=VALUE`) in `args`.
fn flag(args: &[String], name: &str) -> Result<Option<String>, BoxError> {
    let mut iter = args.iter();
    while let Some(arg) = iter.next() {
        if arg == name {
            return iter
                .next()
                .cloned()
                .map(Some)
                .ok_or_else(|| format!("{name} needs a value").into());
        }
        if let Some(value) = arg
            .strip_prefix(name)
            .and_then(|rest| rest.strip_prefix('='))
        {
            return Ok(Some(value.to_owned()));
        }
    }
    Ok(None)
}

/// `serve [--addr HOST:PORT]`: serves until the app's shutdown token is
/// cancelled, then drains in-flight requests for up to `shutdown_grace_secs`.
async fn serve(ctx: AppContext, args: Vec<String>) -> Result<(), BoxError> {
    let server = ctx.get::<HttpServer>().await?;
    let addr = flag(&args, "--addr")?.unwrap_or_else(|| server.options().addr.clone());
    let router = build_router(&ctx).await?;
    let listener = tokio::net::TcpListener::bind(&addr)
        .await
        .map_err(|error| format!("cannot listen on {addr}: {error}"))?;
    let local = listener.local_addr()?;
    server.set_bound(Some(local));
    tracing::info!(target: "navree::http", addr = %local, "listening on http://{local}");

    let result = crate::serve(listener, router)
        .options(server.options())
        .with_graceful_shutdown(ctx.shutdown_token().cancelled_owned())
        .await;
    server.set_bound(None);
    tracing::info!(target: "navree::http", "server stopped");
    Ok(result?)
}

/// `routes [--json]`: prints the route table.
async fn routes(ctx: AppContext, args: Vec<String>) -> Result<(), BoxError> {
    let routes = route_table(&ctx).await?;
    if args.iter().any(|a| a == "--json") {
        println!("{}", serde_json::to_string_pretty(&routes_json(&routes))?);
    } else {
        print!("{}", render_routes(&routes));
    }
    Ok(())
}

/// The route table as text columns: method, path, name, access, module.
pub(crate) fn render_routes(routes: &[RouteMeta]) -> String {
    let mut rows: Vec<[String; 5]> = routes
        .iter()
        .map(|r| {
            [
                r.method.clone(),
                r.full_path.clone(),
                r.name.clone().unwrap_or_default(),
                r.access.describe(),
                r.module.unwrap_or("").to_owned(),
            ]
        })
        .collect();
    rows.sort_by(|a, b| (&a[1], &a[0]).cmp(&(&b[1], &b[0])));
    let header = ["METHOD", "PATH", "NAME", "ACCESS", "MODULE"].map(str::to_owned);
    let mut widths = header.clone().map(|h| h.len());
    for row in &rows {
        for (width, cell) in widths.iter_mut().zip(row) {
            *width = (*width).max(cell.len());
        }
    }
    let mut out = String::new();
    for row in std::iter::once(&header).chain(&rows) {
        let line: Vec<String> = row
            .iter()
            .zip(widths)
            .map(|(cell, width)| format!("{cell:width$}"))
            .collect();
        out.push_str(line.join("  ").trim_end());
        out.push('\n');
    }
    out.push_str(&format!("\n{} routes\n", rows.len()));
    out
}

fn routes_json(routes: &[RouteMeta]) -> serde_json::Value {
    routes
        .iter()
        .map(|r| {
            serde_json::json!({
                "method": r.method,
                "path": r.full_path,
                "name": r.name,
                "access": r.access.describe(),
                "module": r.module,
                "handler": r.handler,
                "throttle": r.throttle.map(|t| serde_json::json!({"limit": t.limit, "per_secs": t.per.as_secs_f64()})),
                "timeout_ms": r.timeout.map(|t| t.as_millis() as u64),
                "body_limit": r.body_limit,
                "transactional": r.transactional,
                "audit": r.audit_action,
                "guards": r.guards.len(),
                "page": r.kind == crate::RouteKind::Page,
            })
        })
        .collect()
}

/// `down [--retry SECS] [--message TEXT]`.
async fn down(ctx: AppContext, args: Vec<String>) -> Result<(), BoxError> {
    let server = ctx.get::<HttpServer>().await?;
    let retry = flag(&args, "--retry")?
        .map(|v| v.parse::<u64>())
        .transpose()
        .map_err(|_| "--retry must be a number of seconds")?;
    let info = MaintenanceInfo {
        time: Some(chrono::Utc::now().timestamp()),
        retry,
        message: flag(&args, "--message")?,
    };
    server.maintenance().down(info).await?;
    println!(
        "The application is in maintenance mode ({}).",
        server.maintenance().path().display()
    );
    Ok(())
}

/// `up`.
async fn up(ctx: AppContext, _args: Vec<String>) -> Result<(), BoxError> {
    let server = ctx.get::<HttpServer>().await?;
    server.maintenance().up().await?;
    println!("The application is up.");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn args(list: &[&str]) -> Vec<String> {
        list.iter().map(|s| (*s).to_owned()).collect()
    }

    #[test]
    fn route_table_renders_sorted_aligned_columns() {
        let route = |method: &str, path: &str, name: Option<&str>, public: bool| {
            let mut meta = RouteMeta {
                method: method.to_owned(),
                full_path: path.to_owned(),
                name: name.map(str::to_owned),
                module: Some("DealsModule"),
                ..RouteMeta::default()
            };
            meta.access.public = public;
            if !public {
                meta.access.roles = vec!["admin".to_owned()];
            }
            meta
        };
        let text = render_routes(&[
            route("POST", "/api/v1/deals", Some("deals.store"), false),
            route("GET", "/api/v1/deals", None, true),
            route("DELETE", "/api/v1/deals/{id}", None, false),
        ]);
        let lines: Vec<&str> = text.lines().collect();
        assert_eq!(
            lines,
            [
                "METHOD  PATH                NAME         ACCESS       MODULE",
                "GET     /api/v1/deals                    public       DealsModule",
                "POST    /api/v1/deals       deals.store  roles:admin  DealsModule",
                "DELETE  /api/v1/deals/{id}               roles:admin  DealsModule",
                "",
                "3 routes",
            ]
        );
        let json = routes_json(&[route("GET", "/x", None, true)]);
        assert_eq!(json[0]["path"], "/x");
        assert_eq!(json[0]["access"], "public");
        assert_eq!(json[0]["handler"], serde_json::Value::Null);
        let mut handled = route("GET", "/y", None, true);
        handled.handler = Some("DealsController::index".to_owned());
        assert_eq!(
            routes_json(&[handled])[0]["handler"],
            "DealsController::index"
        );
        assert_eq!(json[0]["page"], false);
        let mut page = route("GET", "/z", None, true);
        page.kind = crate::RouteKind::Page;
        assert_eq!(routes_json(&[page])[0]["page"], true);
    }

    #[test]
    fn flags_accept_both_forms() {
        assert_eq!(
            flag(&args(&["--addr", "0.0.0.0:1"]), "--addr").unwrap(),
            Some("0.0.0.0:1".to_owned())
        );
        assert_eq!(
            flag(&args(&["--retry=5"]), "--retry").unwrap(),
            Some("5".to_owned())
        );
        assert_eq!(flag(&args(&["--json"]), "--addr").unwrap(), None);
        assert!(flag(&args(&["--addr"]), "--addr").is_err());
    }
}