Thoughts on shaku_rocket service configuration and hand off to rocket #14
-
Hi, I've spent some time tinkering on a basic rocket project here and there the last few days. For context most web services I've worked on in the past have been in C# .NET and Python. Having worked through The Rust Book and some rust exercises here and there I thought a project would make things more concrete and push me to learn more. Starting with Rocket I wanted to find some form of DI configuration to help with testing and swapping out components (Loggers, db etc) while I learn and experiment. Looking at the From the Something like: fn configure_services() -> {
let module = AutoFacModule::builder()
.with_component_parameters::<Logger>(LoggerParameters {
...
})
.with_component_parameters::<Database>(DatabaseParameters {
...
})
.with_component_parameters::<SwaggerUI>(SwaggerParameters {
...
})
....
.build()
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.manage(service)
.mount("/", routes![index])
} This allows for service construction independent of Before I get too far down this path any anti patterns you might call out? Early on in my project and rust learning, so I wanted to get some input. Gist here that extend the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Re reading https://docs.rs/shaku/0.5.0/shaku/macro.module.html this morning I think I would use sub modules for the above. Thanks for giving me a place to think out loud, it's helped some pieces click together more. I'm going to look at the test again too and their structure. If anybody does have suggestions still happy to hear those. |
Beta Was this translation helpful? Give feedback.
-
Building up the services and module can totally be separate from Rocket's initialization. Your gist highlights that pretty well, and I especially like how it includes a test for the endpoint. You found some info on submodules, which is how shaku handles organization and abstraction of groups of services. There's a submodules getting started guide that should be helpful: https://docs.rs/shaku/0.5.0/shaku/guide/submodules/index.html If you want to look at a (medium-large) project which uses |
Beta Was this translation helpful? Give feedback.
Building up the services and module can totally be separate from Rocket's initialization. Your gist highlights that pretty well, and I especially like how it includes a test for the endpoint.
You found some info on submodules, which is how shaku handles organization and abstraction of groups of services. There's a submodules getting started guide that should be helpful: https://docs.rs/shaku/0.5.0/shaku/guide/submodules/index.html
If you want to look at a (medium-large) project which uses
shaku_rocket
, check out https://github.com/Mcat12/pihole-api/. The shaku module is located at https://github.com/Mcat12/pihole-api/blob/master/src/services/mod.rs.