Skip to main content

a3s_boot/module/
mod.rs

1use crate::{
2    BoxFuture, ControllerDefinition, MessagePatternDefinition, Middleware, ModuleRef,
3    ProviderDefinition, ProviderToken, Result, RouteDefinition, WebSocketGatewayDefinition,
4};
5use std::sync::Arc;
6
7mod dynamic;
8
9pub use dynamic::DynamicModule;
10
11/// A module contributes imports, providers, controllers, and routes.
12///
13/// This is the Rust equivalent of a Nest module boundary. Modules organize the
14/// application graph; HTTP serving remains delegated to an adapter.
15pub trait Module: Send + Sync + 'static {
16    /// Stable module name used for deduplication and diagnostics.
17    fn name(&self) -> &'static str;
18
19    /// Imported modules that should be registered before this module.
20    fn imports(&self) -> Vec<Arc<dyn Module>> {
21        Vec::new()
22    }
23
24    /// Providers exported into the application container.
25    fn providers(&self) -> Result<Vec<ProviderDefinition>> {
26        Ok(Vec::new())
27    }
28
29    /// Providers this module exposes to importing modules.
30    fn exports(&self) -> Result<Vec<ProviderToken>> {
31        Ok(Vec::new())
32    }
33
34    /// Whether exported providers should be visible to every module scope.
35    fn is_global(&self) -> bool {
36        false
37    }
38
39    /// Middleware applied to controllers and direct routes declared by this module.
40    fn middleware(&self) -> Vec<Arc<dyn Middleware>> {
41        Vec::new()
42    }
43
44    /// Controller route groups built with access to the provider container.
45    fn controllers(&self, _module_ref: &ModuleRef) -> Result<Vec<ControllerDefinition>> {
46        Ok(Vec::new())
47    }
48
49    /// Framework-neutral routes contributed directly by this module.
50    fn routes(&self) -> Result<Vec<RouteDefinition>> {
51        Ok(Vec::new())
52    }
53
54    /// WebSocket gateways contributed by this module.
55    fn gateways(&self, _module_ref: &ModuleRef) -> Result<Vec<WebSocketGatewayDefinition>> {
56        Ok(Vec::new())
57    }
58
59    /// Microservice message patterns contributed by this module.
60    fn message_patterns(&self, _module_ref: &ModuleRef) -> Result<Vec<MessagePatternDefinition>> {
61        Ok(Vec::new())
62    }
63
64    /// Lifecycle hook called after imports and providers are registered.
65    fn on_module_init(&self, _module_ref: &ModuleRef) -> Result<()> {
66        Ok(())
67    }
68
69    /// Async lifecycle hook called by hosts that want startup work before serve.
70    fn on_application_bootstrap(&self, _module_ref: ModuleRef) -> BoxFuture<'static, Result<()>> {
71        Box::pin(async { Ok(()) })
72    }
73
74    /// Async lifecycle hook called by hosts that need graceful shutdown cleanup.
75    fn on_application_shutdown(&self, _module_ref: ModuleRef) -> BoxFuture<'static, Result<()>> {
76        Box::pin(async { Ok(()) })
77    }
78}