nidus-rs 1.0.15

Public facade crate for the Nidus modular Rust backend framework.
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! High-level application composition for the facade crate.

#[cfg(feature = "dashboard")]
use std::collections::BTreeMap;
use std::collections::BTreeSet;

use nidus_core::{Application, Container, Module, ModuleGraph, Nidus, NidusError, Result};

#[cfg(feature = "dashboard")]
use nidus_dashboard::{
    DashboardGraphEdge, DashboardGraphEdgeKind, DashboardGraphGroup, DashboardGraphNode,
    DashboardGraphNodeKind, DashboardGraphResponse, DashboardRouteSnapshot, NidusDashboard,
};
#[cfg(feature = "http")]
use nidus_http::{
    Router,
    server::{ApplicationHttpExt, HttpApplication},
};
#[cfg(feature = "observability")]
use nidus_observability::{Observability, OperationStatus};
#[cfg(feature = "openapi")]
use nidus_openapi::OpenApiDocument;

/// Extension methods for creating composed Nidus applications from root modules.
pub trait NidusApplicationExt {
    /// Creates an application builder driven by the root module graph.
    fn create<M>() -> NidusApplicationBuilder<M>
    where
        M: Module;
}

impl NidusApplicationExt for Nidus {
    fn create<M>() -> NidusApplicationBuilder<M>
    where
        M: Module,
    {
        NidusApplicationBuilder::new()
    }
}

/// Module-driven application builder.
pub struct NidusApplicationBuilder<M>
where
    M: Module,
{
    container: Container,
    openapi: Option<OpenApiOptions>,
    tracing: bool,
    #[cfg(feature = "http")]
    routers: Vec<Router>,
    #[cfg(feature = "observability")]
    observability: Option<Observability>,
    #[cfg(feature = "dashboard")]
    dashboard: Option<NidusDashboard>,
    #[cfg(feature = "dashboard")]
    dashboard_route_snapshots: Vec<DashboardRouteSnapshot>,
    #[cfg(feature = "openapi")]
    schemas: Vec<fn(OpenApiDocument) -> OpenApiDocument>,
    _module: std::marker::PhantomData<M>,
}

impl<M> NidusApplicationBuilder<M>
where
    M: Module,
{
    fn new() -> Self {
        Self {
            container: Container::new(),
            openapi: None,
            tracing: false,
            #[cfg(feature = "http")]
            routers: Vec::new(),
            #[cfg(feature = "observability")]
            observability: None,
            #[cfg(feature = "dashboard")]
            dashboard: None,
            #[cfg(feature = "dashboard")]
            dashboard_route_snapshots: Vec::new(),
            #[cfg(feature = "openapi")]
            schemas: Vec::new(),
            _module: std::marker::PhantomData,
        }
    }

    /// Adds an externally-created singleton value before module providers initialize.
    pub fn with_singleton<T>(mut self, value: T) -> Result<Self>
    where
        T: Send + Sync + 'static,
    {
        self.container.register_singleton(value)?;
        Ok(self)
    }

    /// Enables generated OpenAPI JSON and docs routes.
    pub fn with_openapi(mut self, title: impl Into<String>, version: impl Into<String>) -> Self {
        self.openapi = Some(OpenApiOptions {
            title: title.into(),
            version: version.into(),
        });
        self
    }

    /// Registers a schema in the generated OpenAPI document.
    #[cfg(feature = "openapi")]
    pub fn with_schema<T>(mut self) -> Self
    where
        T: utoipa::ToSchema,
    {
        self.schemas.push(|document| document.schema::<T>());
        self
    }

    /// Enables the default HTTP tracing middleware on the composed router.
    pub fn with_tracing(mut self) -> Self {
        self.tracing = true;
        self
    }

    /// Applies production observability to the composed HTTP application.
    ///
    /// This merges observability routes such as `/metrics`, applies HTTP
    /// metrics when enabled, and applies the default HTTP tracing layer when
    /// [`Observability::tracing`] was configured.
    #[cfg(feature = "observability")]
    pub fn with_observability(mut self, observability: Observability) -> Self {
        self.observability = Some(observability);
        self
    }

    /// Merges an Axum router into the application built from the root module.
    ///
    /// This is the ergonomic builder-path equivalent of attaching a router
    /// through [`ApplicationHttpExt`] after bootstrapping an [`Application`].
    /// Routes declared by controllers and routes from every router passed here
    /// are composed before tracing, observability, or other builder-owned HTTP
    /// layers are applied.
    #[cfg(feature = "http")]
    pub fn with_router(mut self, router: Router) -> Self {
        self.routers.push(router);
        self
    }

    /// Mounts Nidus Dashboard into the composed HTTP application.
    #[cfg(feature = "dashboard")]
    pub fn with_dashboard(mut self, dashboard: NidusDashboard) -> Self {
        self.dashboard = Some(dashboard);
        self
    }

    /// Builds the application after merging an Axum router.
    ///
    /// This is a convenience for `Nidus::create::<AppModule>()
    /// .with_router(router).build().await`.
    #[cfg(feature = "http")]
    pub async fn build_with_router(self, router: Router) -> Result<HttpApplication> {
        self.with_router(router).build().await
    }

    /// Builds a composed HTTP application.
    #[cfg(feature = "http")]
    pub async fn build(mut self) -> Result<HttpApplication> {
        #[cfg(feature = "observability")]
        let started_at = std::time::Instant::now();
        let graph_result = ModuleGraph::from_root::<M>();
        #[cfg(feature = "observability")]
        if let Some(observability) = &self.observability {
            observability.record_module_graph_validation(
                if graph_result.is_ok() {
                    OperationStatus::Success
                } else {
                    OperationStatus::Failure
                },
                started_at.elapsed(),
            );
        }
        let graph = graph_result?;

        for module in graph.modules() {
            for registrar in module.provider_registrars() {
                registrar(&mut self.container)?;
            }
        }

        for module in graph.modules() {
            for initializer in module.async_initializers() {
                initializer(&mut self.container).await?;
            }
        }

        let router = self.build_router(&graph)?;
        #[cfg(feature = "dashboard")]
        if let Some(dashboard) = &self.dashboard {
            dashboard.record_graph_snapshot(dashboard_graph_from_module_graph(&graph)?);
            for route in self.dashboard_route_snapshots.drain(..) {
                dashboard
                    .record_route_snapshot(route)
                    .await
                    .map_err(|error| NidusError::ApplicationBuild {
                        message: error.to_string(),
                    })?;
            }
        }
        Ok(Application::new(self.container, graph).with_router(router))
    }

    #[cfg(feature = "http")]
    fn build_router(&mut self, graph: &ModuleGraph) -> Result<Router> {
        let mut router = Router::new();
        let mut seen_routes = BTreeSet::new();

        #[cfg(feature = "openapi")]
        let mut openapi = self
            .openapi
            .as_ref()
            .map(|options| OpenApiDocument::new(&options.title, &options.version));

        for module in graph.modules() {
            for controller in module.controller_descriptors() {
                let routes = downcast_routes(controller.route_metadata())?;
                for route in &routes {
                    let full_path = route.try_full_path(controller.prefix()).map_err(|error| {
                        NidusError::ApplicationBuild {
                            message: error.to_string(),
                        }
                    })?;
                    let key = format!("{} {full_path}", route.method());
                    if !seen_routes.insert(key.clone()) {
                        return Err(NidusError::ApplicationBuild {
                            message: format!("duplicate route `{key}`"),
                        });
                    }
                    #[cfg(feature = "dashboard")]
                    self.dashboard_route_snapshots.push(DashboardRouteSnapshot {
                        method: route.method().to_owned(),
                        path: full_path,
                        summary: route.summary().map(str::to_owned),
                        guards: route
                            .guards()
                            .iter()
                            .map(|value| (*value).to_owned())
                            .collect(),
                        pipes: route
                            .pipes()
                            .iter()
                            .map(|value| (*value).to_owned())
                            .collect(),
                        validates: route.validates(),
                    });
                }

                #[cfg(feature = "openapi")]
                if let Some(document) = openapi.take() {
                    openapi = Some(
                        document
                            .try_controller_routes(controller.prefix(), &routes)
                            .map_err(|error| NidusError::ApplicationBuild {
                                message: error.to_string(),
                            })?
                            .schemas_from_route_metadata(&routes),
                    );
                }

                let controller_router = downcast_router(controller.build_router(&self.container)?)?;
                router = router.merge(controller_router);
            }
        }

        for manual_router in self.routers.drain(..) {
            router = router.merge(manual_router);
        }

        #[cfg(feature = "dashboard")]
        if let Some(dashboard) = &self.dashboard {
            router = router.merge(dashboard.mounted_router());
        }

        #[cfg(feature = "openapi")]
        if let Some(mut document) = openapi {
            for schema in &self.schemas {
                document = schema(document);
            }
            router = router.merge(document.into_router());
        }

        #[cfg(feature = "observability")]
        if let Some(observability) = &self.observability {
            router = router.merge(observability.routes());
            if observability.http_metrics_enabled() {
                router = router.layer(observability.http_layer());
            }
        }

        #[cfg(feature = "observability")]
        if let Some(observability) = &self.observability
            && observability.tracing_enabled()
        {
            let mut logging =
                nidus_http::logging::LoggingConfig::production(observability.service_name());
            if let Some(version) = observability.version_label() {
                logging = logging.version(version);
            }
            if let Some(environment) = observability.environment_label() {
                logging = logging.environment(environment);
            }
            return Ok(router.layer(
                tower_http::trace::TraceLayer::new_for_http()
                    .make_span_with(nidus_http::logging::StructuredMakeSpan::new(logging)),
            ));
        }

        if self.tracing {
            router = router.layer(nidus_http::middleware::trace_layer());
        }

        Ok(router)
    }
}

#[cfg(feature = "dashboard")]
fn dashboard_graph_from_module_graph(graph: &ModuleGraph) -> Result<DashboardGraphResponse> {
    let service_name = "nidus-app".to_owned();
    let runtime_id = format!("runtime:{service_name}");
    let mut nodes = Vec::new();
    let mut edges = Vec::new();
    let mut groups = Vec::new();

    let module_ids = graph
        .modules()
        .map(|module| module_node_id(module.name()))
        .collect::<Vec<_>>();

    let mut runtime_counts = BTreeMap::new();
    runtime_counts.insert("modules".to_owned(), module_ids.len());
    runtime_counts.insert(
        "controllers".to_owned(),
        graph
            .modules()
            .map(|module| module.controllers().len())
            .sum(),
    );
    runtime_counts.insert(
        "providers".to_owned(),
        graph.modules().map(|module| module.providers().len()).sum(),
    );

    nodes.push(DashboardGraphNode {
        id: runtime_id.clone(),
        kind: DashboardGraphNodeKind::Runtime,
        label: service_name.clone(),
        summary: Some("validated Nidus module graph".to_owned()),
        status: Some("ready".to_owned()),
        counts: runtime_counts,
        metadata: BTreeMap::from([(
            "root".to_owned(),
            serde_json::Value::String("ModuleGraph".to_owned()),
        )]),
    });
    groups.push(DashboardGraphGroup {
        id: "modules".to_owned(),
        label: "Modules".to_owned(),
        nodes: module_ids,
    });

    for module in graph.modules() {
        let module_id = module_node_id(module.name());
        let mut counts = BTreeMap::new();
        counts.insert("imports".to_owned(), module.imports().len());
        counts.insert("providers".to_owned(), module.providers().len());
        counts.insert("controllers".to_owned(), module.controllers().len());
        counts.insert("exports".to_owned(), module.exports().len());

        nodes.push(DashboardGraphNode {
            id: module_id.clone(),
            kind: DashboardGraphNodeKind::Module,
            label: module.name().to_owned(),
            summary: Some(format!(
                "{} imports / {} controllers",
                module.imports().len(),
                module.controllers().len()
            )),
            status: None,
            counts,
            metadata: BTreeMap::from([
                (
                    "imports".to_owned(),
                    serde_json::to_value(module.imports()).unwrap_or(serde_json::Value::Null),
                ),
                (
                    "providers".to_owned(),
                    serde_json::to_value(module.providers()).unwrap_or(serde_json::Value::Null),
                ),
                (
                    "controllers".to_owned(),
                    serde_json::to_value(module.controllers()).unwrap_or(serde_json::Value::Null),
                ),
                (
                    "exports".to_owned(),
                    serde_json::to_value(module.exports()).unwrap_or(serde_json::Value::Null),
                ),
            ]),
        });
        edges.push(DashboardGraphEdge {
            id: format!("runtime-module:{module_id}"),
            kind: DashboardGraphEdgeKind::RuntimeModule,
            source: runtime_id.clone(),
            target: module_id.clone(),
            label: Some("module".to_owned()),
        });

        for import in module.imports() {
            edges.push(DashboardGraphEdge {
                id: format!("module-import:{module_id}:{import}"),
                kind: DashboardGraphEdgeKind::ModuleImport,
                source: module_id.clone(),
                target: module_node_id(import),
                label: Some("imports".to_owned()),
            });
        }

        for provider in module.providers() {
            let provider_id = provider_node_id(module.name(), provider);
            nodes.push(DashboardGraphNode {
                id: provider_id.clone(),
                kind: DashboardGraphNodeKind::Provider,
                label: provider.clone(),
                summary: Some(format!("provider in {}", module.name())),
                status: None,
                counts: BTreeMap::new(),
                metadata: BTreeMap::from([
                    (
                        "module".to_owned(),
                        serde_json::Value::String(module.name().to_owned()),
                    ),
                    (
                        "exported".to_owned(),
                        serde_json::Value::Bool(module.exports().contains(provider)),
                    ),
                ]),
            });
            edges.push(DashboardGraphEdge {
                id: format!("module-provider:{module_id}:{provider_id}"),
                kind: DashboardGraphEdgeKind::ModuleProvider,
                source: module_id.clone(),
                target: provider_id.clone(),
                label: Some("declares".to_owned()),
            });
            if module.exports().contains(provider) {
                edges.push(DashboardGraphEdge {
                    id: format!("module-export:{module_id}:{provider_id}"),
                    kind: DashboardGraphEdgeKind::ModuleExport,
                    source: module_id.clone(),
                    target: provider_id,
                    label: Some("exports".to_owned()),
                });
            }
        }

        for controller in module.controller_descriptors() {
            let controller_id = controller_node_id(module.name(), controller.name());
            let routes = downcast_routes(controller.route_metadata())?;
            let route_ids = routes
                .iter()
                .map(|route| {
                    let full_path = route.try_full_path(controller.prefix()).map_err(|error| {
                        NidusError::ApplicationBuild {
                            message: error.to_string(),
                        }
                    })?;
                    Ok(route_node_id(route.method(), &full_path))
                })
                .collect::<Result<Vec<_>>>()?;

            nodes.push(DashboardGraphNode {
                id: controller_id.clone(),
                kind: DashboardGraphNodeKind::Controller,
                label: controller.name().to_owned(),
                summary: Some(format!("{} routes", route_ids.len())),
                status: None,
                counts: BTreeMap::from([("routes".to_owned(), route_ids.len())]),
                metadata: BTreeMap::from([
                    (
                        "module".to_owned(),
                        serde_json::Value::String(module.name().to_owned()),
                    ),
                    (
                        "prefix".to_owned(),
                        serde_json::Value::String(controller.prefix().to_owned()),
                    ),
                ]),
            });
            edges.push(DashboardGraphEdge {
                id: format!("module-controller:{module_id}:{controller_id}"),
                kind: DashboardGraphEdgeKind::ModuleController,
                source: module_id.clone(),
                target: controller_id.clone(),
                label: Some("declares".to_owned()),
            });

            for route in routes {
                let full_path = route.try_full_path(controller.prefix()).map_err(|error| {
                    NidusError::ApplicationBuild {
                        message: error.to_string(),
                    }
                })?;
                let route_id = route_node_id(route.method(), &full_path);
                nodes.push(DashboardGraphNode {
                    id: route_id.clone(),
                    kind: DashboardGraphNodeKind::Route,
                    label: format!("{} {full_path}", route.method()),
                    summary: route.summary().map(str::to_owned),
                    status: None,
                    counts: BTreeMap::from([
                        ("guards".to_owned(), route.guards().len()),
                        ("pipes".to_owned(), route.pipes().len()),
                    ]),
                    metadata: BTreeMap::from([
                        (
                            "module".to_owned(),
                            serde_json::Value::String(module.name().to_owned()),
                        ),
                        (
                            "controller".to_owned(),
                            serde_json::Value::String(controller.name().to_owned()),
                        ),
                        (
                            "method".to_owned(),
                            serde_json::Value::String(route.method().to_owned()),
                        ),
                        (
                            "path".to_owned(),
                            serde_json::Value::String(full_path.clone()),
                        ),
                        (
                            "guards".to_owned(),
                            serde_json::to_value(route.guards()).unwrap_or(serde_json::Value::Null),
                        ),
                        (
                            "pipes".to_owned(),
                            serde_json::to_value(route.pipes()).unwrap_or(serde_json::Value::Null),
                        ),
                        (
                            "validates".to_owned(),
                            serde_json::Value::Bool(route.validates()),
                        ),
                    ]),
                });
                edges.push(DashboardGraphEdge {
                    id: format!("controller-route:{controller_id}:{route_id}"),
                    kind: DashboardGraphEdgeKind::ControllerRoute,
                    source: controller_id.clone(),
                    target: route_id,
                    label: Some(route.method().to_owned()),
                });
            }
        }
    }

    Ok(DashboardGraphResponse {
        service_name,
        generated_at_ms: std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|duration| duration.as_millis() as i64)
            .unwrap_or_default(),
        nodes,
        edges,
        groups,
    })
}

#[cfg(feature = "dashboard")]
fn module_node_id(module: &str) -> String {
    format!("module:{module}")
}

#[cfg(feature = "dashboard")]
fn provider_node_id(module: &str, provider: &str) -> String {
    format!("provider:{module}:{provider}")
}

#[cfg(feature = "dashboard")]
fn controller_node_id(module: &str, controller: &str) -> String {
    format!("controller:{module}:{controller}")
}

#[cfg(feature = "dashboard")]
fn route_node_id(method: &str, path: &str) -> String {
    format!("route:{method} {path}")
}

#[derive(Clone, Debug)]
#[allow(dead_code)]
struct OpenApiOptions {
    title: String,
    version: String,
}

#[cfg(feature = "http")]
fn downcast_router(value: Box<dyn std::any::Any + Send + Sync>) -> Result<Router> {
    value
        .downcast::<Router>()
        .map(|router| *router)
        .map_err(|_| NidusError::ApplicationBuild {
            message: "controller returned an unexpected router type".to_owned(),
        })
}

#[cfg(all(feature = "http", feature = "openapi"))]
fn downcast_routes(
    value: Box<dyn std::any::Any + Send + Sync>,
) -> Result<Vec<nidus_http::router::RouteMetadata>> {
    value
        .downcast::<Vec<nidus_http::router::RouteMetadata>>()
        .map(|routes| *routes)
        .map_err(|_| NidusError::ApplicationBuild {
            message: "controller returned unexpected route metadata".to_owned(),
        })
}

#[cfg(all(feature = "http", not(feature = "openapi")))]
fn downcast_routes(
    value: Box<dyn std::any::Any + Send + Sync>,
) -> Result<Vec<nidus_http::router::RouteMetadata>> {
    value
        .downcast::<Vec<nidus_http::router::RouteMetadata>>()
        .map(|routes| *routes)
        .map_err(|_| NidusError::ApplicationBuild {
            message: "controller returned unexpected route metadata".to_owned(),
        })
}