-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_test.go
87 lines (71 loc) · 2.36 KB
/
example_test.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package middleware
import (
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/acronis/go-appkit/log"
)
func Example() {
const errDomain = "MyService"
logger, closeFn := log.NewLogger(&log.Config{Output: log.OutputStdout, Format: log.FormatJSON})
defer closeFn()
router := chi.NewRouter()
router.Use(
RequestID(),
LoggingWithOpts(logger, LoggingOpts{RequestStart: true}),
Recovery(errDomain),
RequestBodyLimit(1024*1024, errDomain),
)
metricsCollector := NewHTTPRequestPrometheusMetrics()
router.Use(HTTPRequestMetricsWithOpts(metricsCollector, getChiRoutePattern, HTTPRequestMetricsOpts{
ExcludedEndpoints: []string{"/metrics", "/healthz"}, // Metrics will not be collected for "/metrics" and "/healthz" endpoints.
}))
userCreateInFlightLimitMiddleware := MustInFlightLimitWithOpts(32, errDomain, InFlightLimitOpts{
GetKey: func(r *http.Request) (string, bool, error) {
key := r.Header.Get("X-Client-ID")
return key, key == "", nil
},
MaxKeys: 1000,
ResponseStatusCode: http.StatusTooManyRequests,
GetRetryAfter: func(r *http.Request) time.Duration {
return time.Second * 15
},
BacklogLimit: 64,
BacklogTimeout: time.Second * 10,
})
usersListRateLimitMiddleware := MustRateLimit(Rate{Count: 100, Duration: time.Second}, errDomain)
router.Route("/users", func(r chi.Router) {
r.With(usersListRateLimitMiddleware).Get("/", func(rw http.ResponseWriter, req *http.Request) {
// Returns list of users.
})
r.With(userCreateInFlightLimitMiddleware).Post("/", func(rw http.ResponseWriter, req *http.Request) {
// Create new user.
})
})
}
// Example of how this function may look for gorilla/mux router is given in the RoutePatternGetterFunc documentation.
// GetChiRoutePattern extracts chi route pattern from request.
func getChiRoutePattern(r *http.Request) string {
// modified code from https://github.com/go-chi/chi/issues/270#issuecomment-479184559
rctx := chi.RouteContext(r.Context())
if rctx == nil {
return ""
}
if pattern := rctx.RoutePattern(); pattern != "" {
// Pattern is already available
return pattern
}
routePath := r.URL.RawPath
if routePath == "" {
routePath = r.URL.Path
}
tctx := chi.NewRouteContext()
if !rctx.Routes.Match(tctx, r.Method, routePath) {
return ""
}
return tctx.RoutePattern()
}