MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / HTTPMiddleware

Function HTTPMiddleware

pkg/ratelimit/http.go:36–50  ·  view source on GitHub ↗

HTTPMiddleware is an HTTP middleware that rate limits by remote IP and the request URL. The remote IP is retrieved by the X-Real-IP header. Use this middleware after webmiddleware.ProxyHeaders()

(limiter Interface, class string)

Source from the content-addressed store, hash-verified

34// HTTPMiddleware is an HTTP middleware that rate limits by remote IP and the request URL.
35// The remote IP is retrieved by the X-Real-IP header. Use this middleware after webmiddleware.ProxyHeaders()
36func HTTPMiddleware(limiter Interface, class string) func(http.Handler) http.Handler {
37 return func(next http.Handler) http.Handler {
38 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39 resource := httpRequestResource(r, class)
40 limit, result := limiter.RateLimit(resource)
41 result.SetHTTPHeaders(w.Header())
42 if limit {
43 webhandlers.Error(w, r, errRateLimitExceeded.WithAttributes("key", resource.Key(), "rate", result.Limit))
44 return
45 }
46
47 next.ServeHTTP(w, r)
48 })
49 }
50}

Callers 11

RegisterRoutesMethod · 0.92
initWebMethod · 0.92
NewFunction · 0.92
NewFunction · 0.92
RegisterRoutesMethod · 0.92
RegisterRoutesMethod · 0.92
NewServerFunction · 0.92
RegisterRoutesMethod · 0.92
RegisterRoutesMethod · 0.92
TestHTTPFunction · 0.92
RegisterRoutesMethod · 0.92

Calls 8

ErrorFunction · 0.92
httpRequestResourceFunction · 0.85
SetHTTPHeadersMethod · 0.80
HeaderMethod · 0.80
RateLimitMethod · 0.65
KeyMethod · 0.65
WithAttributesMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 1

TestHTTPFunction · 0.74