Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Being able to access various things (ip address of the peer or other things) in server interceptors / future middleware implementations in tonic. This is mainly for logging, access control, etc...
This would solve #71.
I need this feature so I went ahead and implemented it in the way described below. It may not fit with what tonic intends to provide in the long term but since it's done, I may as well create this PR.
The implementation has some overlap with #85, since it's inspired by it, but tries to be a bit more flexible.
Solution
Create a
Ctx
struct which is populated according to configuration passed to theServer
builder. This struct is added to theRequest
's extensions so it becomes available to interceptors.Here's what the current usage looks like:
The context struct can be extended with more fields, which can also be gated behind features. This PR only adds a field to read the peer's ip address.
Open questions
Provided the current implementation is acceptable for merging:
Let me know what you think.