- Add bootstrap package to initialize application components including logger, tracer, and HTTP server. - Create config package to load runtime settings from environment variables. - Implement observability features including logging, metrics, and tracing. - Add health check and hello use cases with corresponding HTTP handlers. - Introduce middleware for request ID, access logging, metrics, and recovery. - Set up HTTP router with defined routes for health and hello endpoints. - Include tests for health and hello endpoints to ensure proper functionality. - Add OpenTelemetry collector configuration for trace exporting.
27 lines
557 B
Go
27 lines
557 B
Go
package middleware
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"switchbot-api/internal/transport/http/contextkeys"
|
|
)
|
|
|
|
const RequestIDHeader = "X-Request-Id"
|
|
|
|
func RequestID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
requestID := strings.TrimSpace(c.GetHeader(RequestIDHeader))
|
|
if requestID == "" {
|
|
requestID = uuid.NewString()
|
|
}
|
|
|
|
ctx := contextkeys.WithRequestID(c.Request.Context(), requestID)
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Writer.Header().Set(RequestIDHeader, requestID)
|
|
c.Next()
|
|
}
|
|
}
|