- Add new `app` package to manage application initialization and lifecycle - Refactor `main.go` to use new application management approach - Implement graceful shutdown with context timeout and signal handling - Add dependency injection container initialization - Enhance logging with configurable log levels and structured logging - Update configuration loading and server initialization process - Modify Jitsi configuration in `.env` for custom deployment - Improve error handling and logging throughout application startup - Centralize application startup and shutdown logic in single package Introduces a more robust and flexible application management system with improved initialization, logging, and shutdown capabilities.
38 lines
883 B
Go
38 lines
883 B
Go
package middleware
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// TracingMiddleware adds trace ID to requests for better observability
|
|
func TracingMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Check if trace ID is already provided in headers
|
|
traceID := c.GetHeader("X-Trace-ID")
|
|
|
|
// Generate new trace ID if not provided
|
|
if traceID == "" {
|
|
traceID = generateTraceID()
|
|
}
|
|
|
|
// Set trace ID in context and response header
|
|
c.Set("trace_id", traceID)
|
|
c.Header("X-Trace-ID", traceID)
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// generateTraceID generates a random trace ID
|
|
func generateTraceID() string {
|
|
bytes := make([]byte, 16)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
// Fallback to a simple timestamp-based ID if random generation fails
|
|
return "trace-" + hex.EncodeToString([]byte("fallback"))
|
|
}
|
|
return hex.EncodeToString(bytes)
|
|
}
|