init
This commit is contained in:
commit
fa6a4c650f
93 files changed
+6555
No files matched your search
@@ -0,0 +1,43 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const RequestIDKey contextKey = "request_id"
|
||||
|
||||
func RequestID(
|
||||
next http.Handler,
|
||||
) http.Handler {
|
||||
|
||||
return http.HandlerFunc(
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
id := uuid.NewString()
|
||||
|
||||
ctx := context.WithValue(
|
||||
r.Context(),
|
||||
RequestIDKey,
|
||||
id,
|
||||
)
|
||||
|
||||
w.Header().Set(
|
||||
"X-Request-ID",
|
||||
id,
|
||||
)
|
||||
|
||||
next.ServeHTTP(
|
||||
w,
|
||||
r.WithContext(ctx),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user