-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhelpers.go
More file actions
49 lines (44 loc) · 1.25 KB
/
helpers.go
File metadata and controls
49 lines (44 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package middleware
import (
"net/http"
)
// RoutePatternGetterFunc is a function for getting route pattern from the request. Used in multiple middlewares.
//
// Usually it depends on the router that is used in HTTP server:
//
// func getGorillaMuxRoutePattern(r *http.Request) string {
// curRoute := mux.CurrentRoute(r)
// if curRoute == nil {
// return ""
// }
// pathTemplate, err := curRoute.GetPathTemplate()
// if err != nil {
// return ""
// }
// return pathTemplate
// }
//
// func getChiRoutePattern(r *http.Request) string {
// ctxVal := r.Context().Value(chi.RouteCtxKey)
// if ctxVal == nil {
// return ""
// }
// chiCtx := ctxVal.(*chi.Context)
// if chiCtx == nil {
// return ""
// }
// return chiCtx.RoutePattern()
// }
type RoutePatternGetterFunc func(r *http.Request) string
// WrapResponseWriterIfNeeded wraps an http.ResponseWriter (if it is not already wrapped), returning a proxy that allows you to
// hook into various parts of the response process.
func WrapResponseWriterIfNeeded(rw http.ResponseWriter, protoMajor int) WrapResponseWriter {
if wrw, ok := rw.(WrapResponseWriter); ok {
return wrw
}
return NewWrapResponseWriter(rw, protoMajor)
}