refactor(server): migrate to zerolog, add request logging middleware
This commit is contained in:
@@ -3,15 +3,16 @@ package server
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/fujin/anthropic-proxy/internal/auth"
|
"github.com/fujin/anthropic-proxy/internal/auth"
|
||||||
"github.com/fujin/anthropic-proxy/internal/config"
|
"github.com/fujin/anthropic-proxy/internal/config"
|
||||||
|
"github.com/fujin/anthropic-proxy/internal/logging"
|
||||||
"github.com/fujin/anthropic-proxy/internal/proxy"
|
"github.com/fujin/anthropic-proxy/internal/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@ func New(cfg *config.Config, pool *auth.Pool, profile *proxy.SniffedProfile) *Se
|
|||||||
engine.Use(gin.Recovery())
|
engine.Use(gin.Recovery())
|
||||||
engine.Use(corsMiddleware())
|
engine.Use(corsMiddleware())
|
||||||
engine.Use(s.authMiddleware())
|
engine.Use(s.authMiddleware())
|
||||||
|
engine.Use(logging.GinRequestLogger())
|
||||||
|
|
||||||
handler := proxy.HandleMessages(pool, profile, func() *proxy.Sanitizer {
|
handler := proxy.HandleMessages(pool, profile, func() *proxy.Sanitizer {
|
||||||
return s.sanitizer.Load()
|
return s.sanitizer.Load()
|
||||||
@@ -50,7 +52,7 @@ func New(cfg *config.Config, pool *auth.Pool, profile *proxy.SniffedProfile) *Se
|
|||||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
})
|
})
|
||||||
engine.NoRoute(func(c *gin.Context) {
|
engine.NoRoute(func(c *gin.Context) {
|
||||||
log.Printf("unmatched route: %s %s", c.Request.Method, c.Request.URL.Path)
|
log.Warn().Str("method", c.Request.Method).Str("path", c.Request.URL.Path).Msg("unmatched route")
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -85,8 +87,7 @@ func (s *Server) handleReload() gin.HandlerFunc {
|
|||||||
keys := makeKeySet(cfg.APIKeys)
|
keys := makeKeySet(cfg.APIKeys)
|
||||||
s.apiKeys.Store(&keys)
|
s.apiKeys.Store(&keys)
|
||||||
|
|
||||||
log.Printf("config reloaded: %d tool renames, %d system rules, %d body rules, %d api keys",
|
log.Info().Int("tool_renames", len(cfg.Sanitize.Tools)).Int("system_rules", len(cfg.Sanitize.System)).Int("body_rules", len(cfg.Sanitize.Body)).Int("api_keys", len(cfg.APIKeys)).Msg("config reloaded")
|
||||||
len(cfg.Sanitize.Tools), len(cfg.Sanitize.System), len(cfg.Sanitize.Body), len(cfg.APIKeys))
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"status": "reloaded",
|
"status": "reloaded",
|
||||||
|
|||||||
Reference in New Issue
Block a user