Middleware
Jump to Section
It’s hard to define middleware, but in Mortar’s context you can think of it as proxy/interceptors. Some of them are bundled as part of the libraries Mortar uses, others defined by Mortar.
-
The gRPC package already has a way to register different interceptors for client and server. Mortar already comes with some:
- Server interceptors:
- Logger
UnaryServerInterceptor
, making it easier to log gRPC request and response. - Monitoring
UnaryServerInterceptor
, this one can create automatic Histogram/Timer metric for each gRPC method. - Tracing
UnaryServerInterceptor
, injects or uses tracing information.
- Logger
- Client interceptors:
- Server interceptors:
-
HTTP interceptors:
- Server HTTP middleware.
There are no
http.Handler
interceptors bundled with Mortar. It’s better to use the gRPC layer for that, but you can always add your own. - Client.
Mortar introduces a way to add interceptors to
http.Client
, read here to understand how.- Tracing
HTTPClientInterceptor
, similar to the gRPC one, but for HTTP. - Request/Response dump interceptor, for debugging purposes.
- **In Workshop Demo there is a special TEST Interceptor that returns a custom response.
- Tracing
- Server HTTP middleware.
Other types of middleware
Network interceptors are not the only ones you can use in Mortar. There are other types of middleware.
They rely on context.Context
passed to most methods, read here about that.