Skip to content

Commit

Permalink
chore(volo-http): put client_ip to cx.ext (#541)
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Li <[email protected]>
  • Loading branch information
yukiiiteru authored Nov 29, 2024
1 parent 519c1f4 commit 0d39960
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "volo-http"
version = "0.3.0"
version = "0.3.1"
edition.workspace = true
homepage.workspace = true
repository.workspace = true
Expand Down
12 changes: 5 additions & 7 deletions volo-http/src/server/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,11 @@ impl FromContext for ClientIp {
type Rejection = Infallible;

async fn from_context(cx: &mut ServerContext, _: &mut Parts) -> Result<Self, Self::Rejection> {
Ok(ClientIp(
cx.rpc_info
.caller()
.tags
.get::<ClientIp>()
.and_then(|v| v.0),
))
if let Some(client_ip) = cx.extensions().get::<ClientIp>() {
Ok(client_ip.to_owned())
} else {
Ok(ClientIp(None))
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion volo-http/src/server/utils/client_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ where
req: Request<B>,
) -> Result<Self::Response, Self::Error> {
let client_ip = self.get_client_ip(cx, req.headers());
cx.rpc_info_mut().caller_mut().insert(client_ip);
cx.extensions_mut().insert(client_ip);

self.service.call(cx, req).await
}
Expand Down

0 comments on commit 0d39960

Please sign in to comment.