45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package rest
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// responseWriter wraps http.ResponseWriter to capture status code
|
|
type responseWriter struct {
|
|
http.ResponseWriter
|
|
statusCode int
|
|
}
|
|
|
|
func (rw *responseWriter) WriteHeader(code int) {
|
|
rw.statusCode = code
|
|
rw.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
// loggingMiddleware logs requests with timing
|
|
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
wrapped := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
|
|
|
|
next.ServeHTTP(wrapped, r)
|
|
|
|
duration := time.Since(start)
|
|
// Log request (in production, use structured logger)
|
|
log.Printf("%s %s %d %v", r.Method, r.URL.Path, wrapped.statusCode, duration)
|
|
})
|
|
}
|
|
|
|
// compressionMiddleware adds gzip compression (simplified - use gorilla/handlers in production)
|
|
func (s *Server) compressionMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Check if client accepts gzip
|
|
if r.Header.Get("Accept-Encoding") != "" {
|
|
// In production, use gorilla/handlers.CompressHandler
|
|
// For now, just pass through
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|