-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update changelog. * update test dep. * add example for trait object as application state. * restructure example.
- Loading branch information
1 parent
c9f07a7
commit ecc2fdf
Showing
5 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "dependency-injection" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
xitca-web = "0.2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! example for using trait object as application state. | ||
use std::sync::Arc; | ||
|
||
use xitca_web::{ | ||
handler::{handler_service, state::StateRef}, | ||
route::get, | ||
App, | ||
}; | ||
|
||
fn main() -> std::io::Result<()> { | ||
App::new() | ||
// use trait object as application state | ||
.with_state(object_state()) | ||
// extract trait object in handler | ||
.at("/", get(handler_service(handler))) | ||
.serve() | ||
.bind("127.0.0.1:8080")? | ||
.run() | ||
.wait() | ||
} | ||
|
||
// a simple trait for injecting dependent type and logic to application. | ||
trait DI { | ||
fn name(&self) -> &'static str; | ||
} | ||
|
||
// thread safe trait object state constructor. | ||
fn object_state() -> Arc<dyn DI + Send + Sync> { | ||
// a dummy type implementing DI trait. | ||
struct Foo; | ||
|
||
impl DI for Foo { | ||
fn name(&self) -> &'static str { | ||
"foo" | ||
} | ||
} | ||
|
||
// only this function knows the exact type implementing DI trait. | ||
// everywhere else in the application it's only known as dyn DI trait object. | ||
Arc::new(Foo) | ||
} | ||
|
||
// StateRef extractor is able extract &dyn DI from application state. | ||
async fn handler(StateRef(obj): StateRef<'_, dyn DI + Send + Sync>) -> String { | ||
// get request to "/" path should return "hello foo" string response. | ||
format!("hello {}", obj.name()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters