HTTPRoute is middleware that stores the HTTP route pattern and method in context for use by downstream handlers and services (e.g. prometheus).
(next http.Handler)
| 34 | // HTTPRoute is middleware that stores the HTTP route pattern and method in |
| 35 | // context for use by downstream handlers and services (e.g. prometheus). |
| 36 | func HTTPRoute(next http.Handler) http.Handler { |
| 37 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 38 | route := getRoutePattern(r) |
| 39 | ctx := context.WithValue(r.Context(), httpRouteInfoKey{}, httpRouteInfo{ |
| 40 | Route: route, |
| 41 | Method: r.Method, |
| 42 | }) |
| 43 | next.ServeHTTP(w, r.WithContext(ctx)) |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | func getRoutePattern(r *http.Request) string { |
| 48 | rctx := chi.RouteContext(r.Context()) |