The Trace
middleware that keeps request copy and response records in context.
For example, this middleware can be used to record API requests and responses in the database.
It is also useful when there is no HTTP request in the controller and
only context can be obtained due to limitations of frameworks such as Goa.
If you do not need to record responses, you can use RecordRequest
middleware.
Middleware | Targets | Context Key | Context Object | Option |
---|---|---|---|---|
Trace | Request and Response | TrackerKey | Tracker | DiscardResponseBody |
RecordRequest | Request | RequestRecorderKey | RequestRecorder |
The Trace
middleware used with Goa. Following is an example of a controller:
func (s *calcsrvc) Add(ctx context.Context, p *calc.AddPayload) (res int, err error) {
var header http.Header
tracker, ok := ctx.Value(tracer.TrackerKey).(*tracer.Tracker)
if ok {
header = tracker.Request.Header
}
s.logger.Printf("calc.add, Header: %+v", header)
return p.A + p.B, nil
}
Blog: http://ikawaha.hateblo.jp/entry/2019/12/05/235917
License MIT