Crates.io | more-di-axum |
lib.rs | more-di-axum |
version | 0.2.0 |
source | src |
created_at | 2023-12-20 04:30:49.56133 |
updated_at | 2023-12-24 21:01:24.802017 |
description | Provides support dependency injection (DI) for Axum |
homepage | https://commonsensesoftware.github.io/more-rs-di-axum/ |
repository | https://github.com/commonsensesoftware/more-rs-di-axum |
max_upload_size | |
id | 1075002 |
size | 34,517 |
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:
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();
}
This project is licensed under the MIT license.