todo/internal/middleware/context.go
Michael Thomson f9a8e9666e
Some checks failed
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/lint Pipeline failed
ci/woodpecker/pr/test Pipeline was successful
logging and middleware chaining
2025-05-15 12:34:32 -04:00

25 lines
522 B
Go

package middleware
import (
"context"
"log/slog"
"net/http"
"github.com/google/uuid"
)
type contextKey string
const TraceIdKey contextKey = "trace_id"
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(), TraceIdKey, traceid)
newReq := r.WithContext(ctx)
next.ServeHTTP(w, newReq)
})
}
}