22 lines
457 B
Go
22 lines
457 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func ContextMiddleware(logger *slog.Logger) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
traceid := uuid.NewString()
|
|
ctx := context.WithValue(r.Context(), "trace_id", traceid)
|
|
newReq := r.WithContext(ctx)
|
|
|
|
next.ServeHTTP(w, newReq)
|
|
})
|
|
}
|
|
}
|