-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing around req/resp http::Extensions
Co-Authored-By: Nick Presta <[email protected]>
- Loading branch information
1 parent
3198261
commit d03435d
Showing
10 changed files
with
219 additions
and
34 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
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,42 @@ | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use http::Extensions; | ||
|
||
/// Context allows passing information between twirp rpc handlers and http middleware by providing | ||
/// access to extensions on the `http:Request` and `http:Response`. | ||
/// | ||
/// An example use case is to extract a request id from an http header and use that id in subsequent | ||
/// handler code. | ||
#[derive(Default)] | ||
pub struct Context { | ||
extensions: Extensions, | ||
resp_extensions: Arc<Mutex<Extensions>>, | ||
} | ||
|
||
impl Context { | ||
pub fn new(extensions: Extensions, resp_extensions: Arc<Mutex<Extensions>>) -> Self { | ||
Self { | ||
extensions, | ||
resp_extensions, | ||
} | ||
} | ||
|
||
/// Get a request extension. | ||
pub fn get<T>(&self) -> Option<&T> | ||
where | ||
T: Clone + Send + Sync + 'static, | ||
{ | ||
self.extensions.get::<T>() | ||
} | ||
|
||
/// Insert a response extension. | ||
pub fn insert<T>(&self, val: T) -> Option<T> | ||
where | ||
T: Clone + Send + Sync + 'static, | ||
{ | ||
self.resp_extensions | ||
.lock() | ||
.expect("mutex poisoned") | ||
.insert(val) | ||
} | ||
} |
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
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
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
Oops, something went wrong.