skip

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 3 Imported by: 0

README

Skip Middleware

What it does

Conditionally bypasses another middleware based on a predicate. It is useful for excluding health checks, static assets, or public routes.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/logger"
	"github.com/oarkflow/fh/mw/skip"
)

func main() {
	app := fh.New()
	app.Use(skip.New(logger.New(logger.Config{}), func(c fh.Ctx) bool { return c.Path() == "/health" }))

	app.Get("/health", func(c fh.Ctx) error { return c.Status(fh.StatusOK).SendString("ok") })
}

Impact

Adds a predicate check per request. Helps avoid unnecessary middleware work on excluded routes.

Ordering guidance

Wrap only the middleware that should be skipped. Keep predicates cheap and deterministic.

Production considerations

Avoid broad skips that bypass security accidentally. Add tests for protected and skipped paths.

Documentation

Overview

Package skip conditionally bypasses middleware.

It is useful when middleware should not run for health checks, public routes, CORS preflights, static assets, specific methods, or custom request rules.

Example:

app.Use(skip.New(csrf.New(), skip.Any(
	skip.Paths("/health", "/csrf-token"),
	skip.Methods("GET", "HEAD", "OPTIONS"),
)))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(middleware fh.HandlerFunc, predicate Predicate) fh.HandlerFunc

New wraps middleware and skips it when predicate returns true.

func NewWithConfig

func NewWithConfig(middleware fh.HandlerFunc, cfg Config) fh.HandlerFunc

NewWithConfig wraps middleware using a full skip configuration.

Types

type Config

type Config struct {
	// Predicate is the main predicate. If it returns true, middleware is skipped.
	Predicate Predicate

	// Predicates are additional predicates combined using Logic.
	Predicates []Predicate

	// Logic controls how Predicate + Predicates are combined.
	// Default is LogicAny.
	Logic Logic

	// RecoverPredicatePanic controls whether predicate panics are recovered.
	// Default true. A panicking predicate is treated as "do not skip".
	RecoverPredicatePanic bool

	// OnPredicatePanic is called after a predicate panic is recovered.
	OnPredicatePanic func(fh.Ctx, any)
}

Config configures skip behavior.

type Logic

type Logic uint8

Logic controls how multiple predicates are combined.

const (
	// LogicAny skips when any predicate returns true.
	LogicAny Logic = iota

	// LogicAll skips only when all predicates return true.
	LogicAll
)

type Predicate

type Predicate func(fh.Ctx) bool

Predicate returns true when the wrapped middleware should be skipped.

func All

func All(predicates ...Predicate) Predicate

All skips only when all predicates return true.

func Always

func Always() Predicate

Always always skips middleware.

func Any

func Any(predicates ...Predicate) Predicate

Any skips when any predicate returns true.

func Contains

func Contains(parts ...string) Predicate

Contains skips middleware when request path contains one of the fragments.

func Custom

func Custom(p Predicate) Predicate

Custom wraps a custom predicate while nil-protecting it.

func Globs

func Globs(patterns ...string) Predicate

Globs skips middleware when request path matches one of the glob patterns.

Uses path.Match syntax:

/static/*
/assets/*.css
/api/v?/health

func HeaderContains

func HeaderContains(name, value string) Predicate

HeaderContains skips middleware when a request header contains value.

func HeaderEquals

func HeaderEquals(name, value string) Predicate

HeaderEquals skips middleware when a request header exactly matches value.

func HeaderExists

func HeaderExists(name string) Predicate

HeaderExists skips middleware when a request header exists.

func HeaderPrefix

func HeaderPrefix(name, prefix string) Predicate

HeaderPrefix skips middleware when a request header has prefix.

func HeaderSuffix

func HeaderSuffix(name, suffix string) Predicate

HeaderSuffix skips middleware when a request header has suffix.

func Health

func Health() Predicate

Health skips common health/readiness endpoints.

func Methods

func Methods(methods ...string) Predicate

Methods skips middleware for exact HTTP methods. Method matching is case-insensitive.

func Never

func Never() Predicate

Never never skips middleware.

func Not

func Not(p Predicate) Predicate

Not negates a predicate.

func PathCI

func PathCI(paths ...string) Predicate

PathCI skips middleware for exact path, case-insensitive.

func Paths

func Paths(paths ...string) Predicate

Paths skips middleware for exact request paths.

func Prefixes

func Prefixes(prefixes ...string) Predicate

Prefixes skips middleware when request path has one of the prefixes.

func Preflight

func Preflight() Predicate

Preflight skips middleware for CORS preflight requests.

func QueryContains

func QueryContains(name, value string) Predicate

QueryContains skips middleware when a query parameter contains value.

func QueryEquals

func QueryEquals(name, value string) Predicate

QueryEquals skips middleware when a query parameter exactly matches value.

func QueryExists

func QueryExists(name string) Predicate

QueryExists skips middleware when a query parameter exists.

func SafeMethods

func SafeMethods() Predicate

SafeMethods skips safe/read-only methods.

func Static

func Static() Predicate

Static skips common static asset paths/extensions.

func Suffixes

func Suffixes(suffixes ...string) Predicate

Suffixes skips middleware when request path has one of the suffixes.

Jump to

Keyboard shortcuts

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