2026-02-03 22:40:23 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
2026-08-16 18:12:03 +01:00
|
|
|
"strings"
|
2026-02-03 22:40:23 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-08-16 18:07:39 +01:00
|
|
|
"github.com/git-pkgs/proxy/internal/accesslog"
|
2026-08-16 18:12:03 +01:00
|
|
|
"github.com/git-pkgs/proxy/internal/metrics"
|
2026-02-03 22:40:23 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var requestCounter atomic.Uint64
|
|
|
|
|
|
|
|
|
|
// RequestIDMiddleware adds a sequential request ID to the context and response headers.
|
|
|
|
|
// IDs are formatted as [001], [002], etc. for easy log correlation.
|
|
|
|
|
func RequestIDMiddleware(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
_ = requestCounter.Add(1)
|
|
|
|
|
requestID := middleware.GetReqID(r.Context())
|
|
|
|
|
|
|
|
|
|
// Store formatted ID in context
|
2026-08-16 18:07:39 +01:00
|
|
|
ctx := accesslog.WithRequestID(r.Context(), requestID)
|
2026-02-03 22:40:23 +00:00
|
|
|
|
|
|
|
|
// Add to response header for client tracking
|
|
|
|
|
w.Header().Set("X-Request-ID", requestID)
|
|
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetRequestID retrieves the request ID from context.
|
|
|
|
|
func GetRequestID(ctx context.Context) string {
|
2026-08-16 18:07:39 +01:00
|
|
|
return accesslog.RequestID(ctx)
|
2026-02-03 22:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoggerMiddleware logs HTTP requests with request ID correlation.
|
|
|
|
|
func (s *Server) LoggerMiddleware(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
requestID := GetRequestID(r.Context())
|
|
|
|
|
|
|
|
|
|
rw := &responseWriter{ResponseWriter: w, status: http.StatusOK}
|
|
|
|
|
next.ServeHTTP(rw, r)
|
2026-08-16 18:12:03 +01:00
|
|
|
duration := time.Since(start)
|
2026-02-03 22:40:23 +00:00
|
|
|
|
|
|
|
|
s.logger.Info("request",
|
|
|
|
|
"request_id", requestID,
|
|
|
|
|
"method", r.Method,
|
|
|
|
|
"path", r.URL.Path,
|
|
|
|
|
"status", rw.status,
|
2026-08-16 18:12:03 +01:00
|
|
|
"duration", duration,
|
2026-02-03 22:40:23 +00:00
|
|
|
"remote", r.RemoteAddr)
|
2026-08-16 18:07:39 +01:00
|
|
|
|
2026-08-16 18:12:03 +01:00
|
|
|
if r.URL.Path != "/metrics" {
|
|
|
|
|
metrics.RecordRequest(requestEcosystem(r.URL.Path), rw.status, duration)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 18:07:39 +01:00
|
|
|
if s.accessLog != nil {
|
|
|
|
|
if err := s.accessLog.Write(accesslog.Entry{
|
|
|
|
|
Event: accesslog.EventRequest,
|
|
|
|
|
RequestID: requestID,
|
|
|
|
|
Method: r.Method,
|
|
|
|
|
Path: r.URL.EscapedPath(),
|
|
|
|
|
StatusCode: rw.status,
|
2026-08-16 18:12:03 +01:00
|
|
|
DurationMS: duration.Milliseconds(),
|
2026-08-16 18:07:39 +01:00
|
|
|
RemoteAddr: r.RemoteAddr,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
s.logger.Error("failed to write access log", "error", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-03 22:40:23 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 18:12:03 +01:00
|
|
|
func requestEcosystem(path string) string {
|
|
|
|
|
segment, _, _ := strings.Cut(strings.TrimPrefix(path, "/"), "/")
|
|
|
|
|
switch segment {
|
|
|
|
|
case "npm", "cargo", "hex", "pub", "pypi", "maven", "gradle", "nuget",
|
|
|
|
|
"conan", "conda", "cran", "julia", "debian", "rpm":
|
|
|
|
|
return segment
|
|
|
|
|
case "gem":
|
|
|
|
|
return "rubygems"
|
|
|
|
|
case "go":
|
|
|
|
|
return "golang"
|
|
|
|
|
case "composer":
|
|
|
|
|
return "packagist"
|
|
|
|
|
case "v2":
|
|
|
|
|
return "oci"
|
|
|
|
|
default:
|
|
|
|
|
return "other"
|
|
|
|
|
}
|
|
|
|
|
}
|