Skip to main content

Crate injectium_salvo

Crate injectium_salvo 

Source
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 resolves T from the container in your handlers
  • inject_container - Registers the container into Salvo’s request scope

§Features

  • oapi - Enable OpenAPI support for Injected<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 Container into the Salvo Depot so that Injected<T> can resolve dependencies from it.