Skip to content

Commit

Permalink
Log 404s too
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Feb 15, 2025
1 parent 44d327b commit fe2c4bb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cmd/hubproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ func logMiddleware(listenerType string, next http.Handler) http.Handler {
})
}

func wrapMuxWithNotFound(listenerType string, mux *http.ServeMux) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h, pattern := mux.Handler(r)
if pattern == "" {
slog.Info("handled request",
"listener", listenerType,
"method", r.Method,
"path", r.URL.Path,
"remote_addr", r.RemoteAddr,
)
http.NotFound(w, r)
return
}
h.ServeHTTP(w, r)
})
}

func run() error {
ctx := context.Background()

Expand Down Expand Up @@ -192,7 +209,7 @@ func run() error {
webhookMux := http.NewServeMux()
webhookMux.Handle("/webhook", logMiddleware("webhook", webhookHandler))
webhookSrv := &http.Server{
Handler: webhookMux,
Handler: wrapMuxWithNotFound("webhook", webhookMux),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
Expand All @@ -208,7 +225,7 @@ func run() error {
apiMux.Handle("/api/replay", logMiddleware("api", http.HandlerFunc(apiHandler.ReplayRange))) // Handle range replay
apiMux.Handle("/metrics", logMiddleware("api", promhttp.Handler())) // Add Prometheus metrics endpoint
apiSrv := &http.Server{
Handler: apiMux,
Handler: wrapMuxWithNotFound("api", apiMux),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
Expand Down

0 comments on commit fe2c4bb

Please sign in to comment.