Crates.io | runtime_injector_actix |
lib.rs | runtime_injector_actix |
version | 0.2.0 |
source | src |
created_at | 2021-05-08 03:02:44.258262 |
updated_at | 2021-05-14 06:53:01.426818 |
description | Runtime dependency injection container for actix-web |
homepage | |
repository | https://github.com/TehPers/runtime_injector |
max_upload_size | |
id | 394700 |
size | 52,083 |
This library provides types to help with injecting services into actix-web applications.
Create your injector, then add it as app data to your application:
#[actix::main]
async fn main() -> std::io::Result<()> {
// Define a module so the container knows what to inject
let module = define_module! {
#[cfg(not(test))]
services = [
UserAuthenticator::new.transient(),
],
#[cfg(not(debug_assertions))]
interfaces = {
dyn UserDatabase = [SqlUserDatabase::new.singleton()],
},
#[cfg(debug_assertions)]
interfaces = {
dyn UserDatabase = [JsonUserDatabase::new.singleton()],
},
};
// Configure and build the container
let mut builder = Injector::builder();
builder.add_module(module);
let injector = builder.build();
// Now add it as app data to the application
HttpServer::new(|| App::new().app_data(injector.clone()).service(index))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Inject dependencies with Injected<R>
in your request handlers:
#[get("/")]
async fn index(
user_db: Injected<Svc<dyn UserDatabase>>,
user_auth: Injected<Box<UserAuthenticator>>,
) -> impl Responder {
todo!()
}
As the library is still in development, the only supported Rust version is the most recent version of stable Rust. The library may work on older versions, but there is no guarantee.
This library is licensed under your choice of either MIT or Apache 2.0.