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

Function Conditional

pkg/webmiddleware/webmiddleware.go:34–44  ·  view source on GitHub ↗

Conditional is a middleware that only executes middleware if the condition returns true for the request. If the condition returns false, the middleware is skipped, and request handling moves on to the next handler in the chain.

(middleware MiddlewareFunc, condition func(r *http.Request) bool)

Source from the content-addressed store, hash-verified

32// returns true for the request. If the condition returns false, the middleware
33// is skipped, and request handling moves on to the next handler in the chain.
34func Conditional(middleware MiddlewareFunc, condition func(r *http.Request) bool) MiddlewareFunc {
35 return func(next http.Handler) http.Handler {
36 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37 handler := next
38 if condition(r) {
39 handler = middleware(next)
40 }
41 handler.ServeHTTP(w, r)
42 })
43 }
44}

Callers 1

TestConditionalFunction · 0.85

Calls 1

ServeHTTPMethod · 0.45

Tested by 1

TestConditionalFunction · 0.68