MCPcopy Create free account
hub / github.com/kataras/iris / NewConditionalHandler

Function NewConditionalHandler

context/handler.go:319–348  ·  view source on GitHub ↗

NewConditionalHandler returns a single Handler which can be registered as a middleware. Filter is just a type of Handler which returns a boolean. Handlers here should act like middleware, they should contain `ctx.Next` to proceed to the next handler of the chain. Those "handlers" are registered to t

(filter Filter, handlers ...Handler)

Source from the content-addressed store, hash-verified

317//
318// Example can be found at: _examples/routing/conditional-chain.
319func NewConditionalHandler(filter Filter, handlers ...Handler) Handler {
320 return func(ctx *Context) {
321 if filter(ctx) {
322 // Note that we don't want just to fire the incoming handlers, we must make sure
323 // that it won't break any further handler chain
324 // information that may be required for the next handlers.
325 //
326 // The below code makes sure that this conditional handler does not break
327 // the ability that iris provides to its end-devs
328 // to check and modify the per-request handlers chain at runtime.
329 currIdx := ctx.HandlerIndex(-1)
330 currHandlers := ctx.Handlers()
331
332 if currIdx == len(currHandlers)-1 {
333 // if this is the last handler of the chain
334 // just add to the last the new handlers and call Next to fire those.
335 ctx.AddHandler(handlers...)
336 ctx.Next()
337 return
338 }
339 // otherwise insert the new handlers in the middle of the current executed chain and the next chain.
340 newHandlers := append(currHandlers[:currIdx+1], append(handlers, currHandlers[currIdx+1:]...)...)
341 ctx.SetHandlers(newHandlers)
342 ctx.Next()
343 return
344 }
345 // if not pass, then just execute the next.
346 ctx.Next()
347 }
348}
349
350// JoinHandlers returns a copy of "h1" and "h2" Handlers slice joined as one slice of Handlers.
351func JoinHandlers(h1 Handlers, h2 Handlers) Handlers {

Callers

nothing calls this directly

Calls 5

HandlerIndexMethod · 0.80
AddHandlerMethod · 0.80
NextMethod · 0.80
SetHandlersMethod · 0.80
HandlersMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…