24 lines
494 B
Go
24 lines
494 B
Go
package middlewares
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type RequestIdMiddleware struct{}
|
|
|
|
func NewRequestMiddleware() *RequestIdMiddleware {
|
|
return &RequestIdMiddleware{}
|
|
}
|
|
|
|
func (m *RequestIdMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
requestId := r.Header.Get("X-Request-Id")
|
|
if requestId != "" {
|
|
ctx = context.WithValue(ctx, "request_id", requestId)
|
|
}
|
|
next(w, r.WithContext(ctx))
|
|
}
|
|
}
|