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 README with proper request and response usage #14

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@ tonic-middleware = "0.2.0"

See full [example](https://github.com/teimuraz/tonic-middleware/tree/main/example) or check [integration tests](https://github.com/teimuraz/tonic-middleware/blob/main/integration_tests/tests/tests.rs)


### Define our request interceptor and middleware

#### To create request interceptor, we need to implement `RequestInterceptor` trait from the library.
### Note:
> Please use `tonic::codegen::http::{Request, Response}` (which are just re-exported from `http` crate by tonic)
> instead of `tonic::{Request, Response}` in interceptors and middlewares.


Simple request interceptor that uses some custom `AuthService` injected in to perform authentication.
We need to implement `RequestInterceptor` for our custom (`AuthInterceptor`) intercept.
```rust
use tonic::codegen::http::Request; // Use this instead of tonic::Request in Interceptor!
use tonic::codegen::http::Respons; // Use this instead of tonic::Response in Interceptor!
...

#[derive(Clone)]
pub struct AuthInterceptor<A: AuthService> {
pub auth_service: A,
Expand Down Expand Up @@ -101,6 +110,9 @@ impl<A: AuthService> RequestInterceptor for AuthInterceptor<A> {
Metrics middleware that measures request time and output to stdout.
We need to implement `Middleware` for our custom (`MetricsMiddleware`) middleware.
```rust
use tonic::codegen::http::Request; // Use this instead of tonic::Request in Middleware!
use tonic::codegen::http::Response; // Use this instead of tonic::Response in Middleware!
...

#[derive(Default, Clone)]
pub struct MetricsMiddleware;
Expand Down