Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update changelog. #911

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"cloudflare-worker",
"compression",
"cookie",
"dependency-injection",
"error-handle",
"file",
"grpc",
Expand Down
7 changes: 7 additions & 0 deletions examples/dependency-injection/Cargo.toml
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"
48 changes: 48 additions & 0 deletions examples/dependency-injection/src/main.rs
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())
}
4 changes: 2 additions & 2 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ io-uring = ["xitca-http/io-uring", "xitca-server/io-uring"]

[dependencies]
xitca-client = { version = "0.1", features = ["http2", "http3", "websocket", "dangerous"] }
xitca-http = { version = "0.2", features = ["http2", "http3"] }
xitca-http = { version = "0.2.1", features = ["http2", "http3"] }
xitca-codegen = "0.1"
xitca-io = "0.1"
xitca-server = { version = "0.1", features = ["http3"] }
xitca-service = "0.1"
xitca-unsafe-collection = "0.1"
xitca-web = "0.2"
xitca-web = "0.2.2"

http-ws = { version = "0.2", features = ["stream"] }

Expand Down
4 changes: 3 additions & 1 deletion web/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# unreleased 0.2.2
# unreleased

# 0.2.2
## Add
- `StateRef` can used for extracting `?Sized` type from application state.

Expand Down
Loading