conditional

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Conditional middleware: a debug bundle that runs only when the request asks for it.

iris.NewConditionalHandler(filter, handlers...) packs a filter and one or more middleware into a single registerable handler. The filter is an iris.Filter - func(iris.Context) bool. When it returns true the handlers are spliced into the current request's chain and run in order; when it returns false the chain simply continues without them. The wrapped handlers must behave like middleware, i.e. call ctx.Next().

Here a debug bundle (request echo + timing log) is gated on an "X-Debug: 1" request header, so its cost is paid only when someone asks for it.

For coarse conditions you rarely need this: attach the middleware to the child router or subdomain whose requests it concerns and let the routing structure do the filtering. Reach for a conditional handler when the decision is genuinely per-request.

Learn: gate one or more middleware on a per-request iris.Filter with iris.NewConditionalHandler. Before: ../writing-middleware

Run:

go run main.go

Try:

curl -i http://localhost:8080/items                           # 200, no X-Debug-* headers
curl -i -H "X-Debug: 1" http://localhost:8080/items           # 200, X-Debug-* headers
curl -i -X POST -H "X-Debug: 1" http://localhost:8080/items   # 201, X-Debug-Method: POST

Next: ../error-middleware

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL