Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fc00101aa |
@@ -10,8 +10,6 @@ import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// responseWriter wraps http.ResponseWriter to capture the response
|
||||
@@ -283,16 +281,3 @@ func RequestLoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handle
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// DecodedPathMiddleware makes the router match routes against the decoded
|
||||
// path, so that the handlers read their URL parameters decoded. Without it,
|
||||
// chi matches against the escaped path whenever the request has one and hands
|
||||
// the handlers their parameters still escaped: /ip/2001%3Adb8%3A%3A1 would
|
||||
// look up "2001%3Adb8%3A%3A1".
|
||||
func DecodedPathMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
chi.RouteContext(r.Context()).RoutePath = r.URL.Path
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -12,7 +13,18 @@ func (s *Server) setupRoutes() {
|
||||
r := chi.NewRouter()
|
||||
|
||||
// Middleware
|
||||
r.Use(DecodedPathMiddleware)
|
||||
|
||||
// Match routes against the decoded path, so that the handlers read their
|
||||
// URL parameters decoded. chi otherwise matches against the escaped path
|
||||
// when the request has one and leaves the parameters escaped:
|
||||
// /ip/2001%3Adb8%3A%3A1 would look up "2001%3Adb8%3A%3A1".
|
||||
r.Use(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
chi.RouteContext(req.Context()).RoutePath = req.URL.Path
|
||||
|
||||
next.ServeHTTP(w, req)
|
||||
})
|
||||
})
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(RequestLoggerMiddleware(s.logger.Logger)) // Structured request logging
|
||||
|
||||
Reference in New Issue
Block a user