Clean public release with: - Go/Gin/PostgreSQL/Redis architecture - Multi-tenant support - JWT authentication - Docker Compose deployment - Environment-based configuration
39 lines
734 B
Go
39 lines
734 B
Go
package middleware
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func RequestID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
traceID := c.GetHeader("X-Request-ID")
|
|
if traceID == "" {
|
|
traceID = uuid.New().String()
|
|
}
|
|
c.Set("trace_id", traceID)
|
|
c.Header("X-Request-ID", traceID)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func RequestLogger() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
path := c.Request.URL.Path
|
|
|
|
c.Next()
|
|
|
|
latency := time.Since(start)
|
|
status := c.Writer.Status()
|
|
gin.DefaultWriter.Write([]byte(
|
|
"[" + c.GetString("trace_id") + "] " +
|
|
c.Request.Method + " " + path + " " +
|
|
strconv.Itoa(status) + " " + latency.String() + "\n",
|
|
))
|
|
}
|
|
}
|