Crate di_axum

Crate di_axum 

Source
Expand description

§More DI for Axum   CI Crates.io MIT licensed

More DI is a dependency injection (DI) library for Rust. This library provides additional DI extensions for the axum web framework.

You may be looking for:

§Dependency Injection in Axum

Consider the following structure.

use di::*;

#[injectable]
struct Person;

impl Person {
    fn speak(&self) -> &str {
        "Hello world!"
    }
}

This information can now be composed into a web application:

use crate::*;
use di::*;
use di_axum::*;

async fn say_hello(Inject(person): Inject<Person>) -> String {
    person.speak().to_owned()
}

#[tokio::main]
async fn main() {
    let provider = ServiceCollection::new()
        .add(Person::scoped())
        .build_provider()
        .unwrap();

    let app = Router::new()
        .route("/hello", get(say_hello))
        .with_provider(provider);

    let listener = TcpListener::bind("127.0.0.1:5000").await.unwrap();

    println!("Now listening on: {}", listener.local_addr().unwrap());

    axum::serve(listener, app).await.unwrap();
}

§License

This project is licensed under the MIT license.

Structs§

Inject
Represents a container for a required, injected service.
InjectAll
Represents a container for a collection of injected services.
InjectAllMut
Represents a container for a collection of mutable, injected services.
InjectAllWithKey
Represents a container for a collection of injected, keyed services.
InjectAllWithKeyMut
Represents a container for a collection of mutable, injected, keyed services.
InjectMut
Represents a container for a required, mutable, injected service.
InjectWithKey
Represents a container for a required, injected, keyed service.
InjectWithKeyMut
Represents a container for a required, mutable, injected, keyed service.
TryInject
Represents a container for an optional, injected service.
TryInjectMut
Represents a container for an optional, mutable, injected service.
TryInjectWithKey
Represents a container for an optional, injected, keyed service.
TryInjectWithKeyMut
Represents a container for an optional, mutable, injected, keyed service.

Traits§

RouterServiceProviderExtensions
Provides axum::Router extension methods.