Modules
Modules are the primary way to organize an application into coherent units. A module brings together controllers, providers, and dependencies on other modules, and controls what it exposes to the rest of the world.
#[module]
use ferrite_framework::{module, Module};
#[module(
imports = [AuthModule],
controllers = [UsersController],
providers = [UsersService, UsersRepository],
exports = [UsersService],
)]
pub struct UsersModule;| Key | Meaning |
|---|---|
imports | Other modules whose exported providers become resolvable here |
controllers | Controller structs whose routes are registered |
providers | Providers instantiated and made available within this module |
exports | Providers re-exported to modules that import this one |
global | Flag marking all exports as globally available |
Only unit structs are supported:
pub struct UsersModule;The macro type-asserts everything: imported names must implement Module, and providers/controllers/exports must implement Injectable. Mistakes become compiler errors.
The root module
Every application has one root module that imports everything else:
use ferrite_framework::{module, Ferrite};
#[module(
imports = [AuthModule, HealthModule, SwaggerModule, UsersModule],
)]
pub struct AppModule;
#[bootstrap]
async fn main() {
let app = Ferrite::create::<AppModule>().await;
app.listen("0.0.0.0:3000").await.expect("failed to bind");
}The module descriptor
At compile time the macro emits a ModuleDescriptor:
pub struct ModuleDescriptor {
pub name: String,
pub providers: Vec<TypeId>,
pub controllers: Vec<TypeId>,
pub imports: Vec<TypeId>,
pub exports: Vec<TypeId>,
pub middleware: Vec<TypeId>,
pub global: bool,
}Discovery helpers walk these descriptors at runtime:
use ferrite_framework::{all_modules, find_module, all_providers, lookup_provider};
all_modules() // Vec<&ModuleDescriptor>
find_module("UsersModule") // Option<&ModuleDescriptor>
all_providers() // Vec<&ProviderEntry>
lookup_provider::<UsersService>() // Unique lookup by typeModule providers and global registration
When a module lists a provider in providers or exports, it contributes a ProviderEntry to the inventory registry. Providers with no module — like infrastructure singletons — register themselves globally.
Modules from ecosystem crates generally expose a ready-made struct:
#[module(imports = [AuthModule, HealthModule, SwaggerModule, CacheModule, ThrottlerModule])]
pub struct AppModule;Extending modules with ecosystem modules
Features ship as modules + providers together. For example:
AuthModule— JWTJwtService,AuthGuard,CurrentUserHealthModule—HealthService,HealthControllerat/healthSwaggerModule—SwaggerService, Swagger UI at/docsCacheModule—CacheServiceThrottlerModule—ThrottlerService,ThrottlerGuard
Next steps
- Bootstrap — how
Ferrite::createcompiles the graph. - Dependency injection.
- Testing — override providers in a
TestingModule.