Web framework based on Springtime application framework and axum. Inspired by the Spring Framework in Java, Springtime Web Axum provides a way to create advanced modular Rust web applications by ensuring all components of the application are properly decoupled from each other, and are managed by the dependency injection system.
While axum
provides a way to explicitly create web handlers in an imperative
way, this crate gives the option to create multi-layer applications in a
declarative way, leveraging underlying dependency injection. This enables rapid
application development and loose coupling between components.
axum
Springtime Web Axum is highly configurable, but the most basic usage example
is quite simple and consists of declaring controllers, creating an Application
instance and calling run()
. For tutorial, advanced features, and patterns,
please look at the examples,
which form a step-by-step guide.
The following example assumes familiarity with springtime and springtime-di.
```rust use axum::extract::Path; use springtime::application; use springtimedi::instanceprovider::ComponentInstancePtr; use springtimedi::{injectable, Component, componentalias}; use springtimewebaxum::controller;
// injectable example trait representing a domain service
trait DomainService { fn getimportantmessage(&self, user: &str) -> String; }
// concrete service implementation
struct ExampleDomainService;
// register ExampleDomainService as providing dyn DomainService
impl DomainService for ExampleDomainService { fn getimportantmessage(&self, user: &str) -> String { format!("Hello {}!", user) } }
// create a struct which will serve as our Controller - this implies it needs to be a Component for // the dependency injection to work
struct ExampleController {
// inject the domain service (alternatively, inject concrete type instead of a trait)
service: ComponentInstancePtr
// mark the struct as a Controller - this will scan all functions for the controller attributes and // create axum handlers out of them
impl ExampleController { // this function will respond to GET request for http://localhost/ (or any network interface) #[get("/")] async fn hello_world(&self) -> &'static str { "Hello world!" }
// all axum features are available for controllers
#[get("/:user")]
async fn hello_user(&self, Path(user): Path<String>) -> String {
// delegate work to our domain service
self.service.get_important_message(&user)
}
}
// note: for the sake of simplicity, errors are unwrapped, rather than gracefully handled
async fn main() { let mut application = application::create_default().expect("unable to create application");
// run our server with default configuration - requests should be forwarded to ExampleController
application.run().await.expect("error running application");
} ```