Files
orbits/internal/api/middleware/middleware.go
T
2026-04-23 14:57:02 +02:00

47 lines
942 B
Go

package middleware
import (
"log/slog"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func SlogMiddleware(logger *slog.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
logger.Debug("request",
slog.String("method", c.Request.Method),
slog.String("path", path),
slog.String("query", query),
slog.Int("status", c.Writer.Status()),
slog.Duration("latency", time.Since(start)),
slog.String("client_ip", c.ClientIP()),
slog.Int("body_size", c.Writer.Size()),
)
if len(c.Errors) > 0 {
for _, err := range c.Errors {
logger.Error("request error", slog.String("error", err.Error()))
}
}
}
}
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
orbitsKey := c.GetHeader("orbits-key")
if len(orbitsKey) == 0 {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
}
}