- Webhook event logging repo and subscription webhook idempotency - Pagination helper (echohelpers) with cursor/offset support - Request ID and structured logging middleware - Push client improvements (FCM HTTP v1, better error handling) - Task model version column, business constraint migrations, targeted indexes - Expanded categorization chain tests - Email service and config hardening - CI workflow updates, .gitignore additions, .env.example updates Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
const (
|
|
// HeaderXRequestID is the header key for request correlation IDs
|
|
HeaderXRequestID = "X-Request-ID"
|
|
// ContextKeyRequestID is the echo context key for the request ID
|
|
ContextKeyRequestID = "request_id"
|
|
)
|
|
|
|
// RequestIDMiddleware generates a UUID per request, sets it as X-Request-ID header,
|
|
// and stores it in the echo context for downstream use.
|
|
func RequestIDMiddleware() echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
// Use existing request ID from header if present, otherwise generate one
|
|
reqID := c.Request().Header.Get(HeaderXRequestID)
|
|
if reqID == "" {
|
|
reqID = uuid.New().String()
|
|
}
|
|
|
|
// Store in context
|
|
c.Set(ContextKeyRequestID, reqID)
|
|
|
|
// Set response header
|
|
c.Response().Header().Set(HeaderXRequestID, reqID)
|
|
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetRequestID extracts the request ID from the echo context
|
|
func GetRequestID(c echo.Context) string {
|
|
if id, ok := c.Get(ContextKeyRequestID).(string); ok {
|
|
return id
|
|
}
|
|
return ""
|
|
}
|