blob: 9a49239a6e2a10c3a6699df87d20341e9c827701 (
plain) (
tree)
|
|
package middleware
import (
"context"
"net/http"
"neonxp.ru/go/mux/ctxlib"
"neonxp.ru/go/objectid"
)
const RequestIDHeader string = "X-Request-ID"
func RequestID(next http.Handler) http.Handler {
objectid.Seed()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get(RequestIDHeader)
if requestID == "" {
requestID = objectid.New().String()
}
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxlib.RequestID, requestID)))
})
}
func GetRequestID(r *http.Request) string {
rid := r.Context().Value(ctxlib.RequestID)
if rid == nil {
return ""
}
srid, ok := rid.(string)
if !ok {
return ""
}
return srid
}
|