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)
| 317 | // |
| 318 | // Example can be found at: _examples/routing/conditional-chain. |
| 319 | func 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. |
| 351 | func JoinHandlers(h1 Handlers, h2 Handlers) Handlers { |
nothing calls this directly
no test coverage detected
searching dependent graphs…