Expand description
Salvo integration for Injectium dependency injection.
This crate provides seamless integration between the Injectium dependency injection container and the Salvo web framework.
§Quick Start
§1. Define your services with #[derive(Injectable)]
ⓘ
use injectium::Injectable;
#[derive(Clone)]
struct Database {
connection_string: String,
}
#[derive(Clone, Injectable)]
struct UserService {
db: Database,
}§2. Build the container
ⓘ
use injectium::{cloned, container};
use std::sync::Arc;
let container = Arc::new(container! {
providers: [
cloned(Database { connection_string: "postgres://localhost".into() }),
],
});§3. Configure Salvo router
ⓘ
use salvo::prelude::*;
use injectium_salvo::{inject_container, Injected};
#[handler]
async fn get_users(users: Injected<UserService>) -> String {
format!("Users DB: {}", users.db.connection_string)
}
let router = Router::new()
.hoop(inject_container(container))
.push(Router::with_path("/users").get(get_users));§Key Types
Injected<T>- Automatically resolvesTfrom the container in your handlersinject_container- Registers the container into Salvo’s request scope
§Features
oapi- Enable OpenAPI support forInjected<T>types
§Error Handling
If the container is not registered or a dependency is missing, the handler
will panic with a helpful message. Use Container::validate at startup to
catch missing dependencies early.
§Thread Safety
All injected types must implement Send + Sync + 'static to be safely
shared across async tasks.
§Example
See the injectium examples for a complete working example.
Structs§
- Injected
- A wrapper type for injecting dependencies from an Injectium container into Salvo handlers.
Functions§
- inject_
container - Register a
Containerinto the SalvoDepotso thatInjected<T>can resolve dependencies from it.