router

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

type Chain struct {
	// contains filtered or unexported fields
}

func NewChain

func NewChain(h http.Handler) *Chain

NewChain creates a new Chain instance with the base handler and initialized middlewares slice.

func (*Chain) Handler

func (r *Chain) Handler() http.Handler

Handler returns the final handler with all middlewares and observers applied

func (*Chain) WithMiddleware

func (r *Chain) WithMiddleware(middlewares ...func(http.Handler) http.Handler) *Chain

WithMiddleware adds one or more middlewares to the chain. Middlewares execute in the order they are defined, from left to right. For example:

.WithMiddleware(mw1, mw2, mw3)

Will execute as: 1. mw1 (first middleware runs first) 2. mw2 3. mw3 4. Handler

This follows the same semantics as popular middleware chaining packages like Alice (github.com/justinas/alice) where the first middleware in the chain is the outermost handler that runs first. This matches the natural reading order of the code and makes it easier to reason about middleware execution.

func (*Chain) WithMiddlewareChain

func (r *Chain) WithMiddlewareChain(middlewares []func(http.Handler) http.Handler) *Chain

WithMiddlewareChain prepends a chain of middlewares (added in given order)

func (*Chain) WithObservers

func (r *Chain) WithObservers(observers ...http.Handler) *Chain

WithObservers adds handlers that run after the handler and middleware chain. Observers are typically used for logging, metrics collection, and other side effects. Note that observers will execute even if middleware returns early or stops processing. Observers should not write to the response as the main handler may have already sent headers. Use carefully as this could lead to unintended side effects when middleware fails.

type Chains

type Chains map[string]*Chain

Chains represents a collection of route paths mapped to their handler Chains.

type Router

type Router interface {
	Handle(string, http.Handler)
	HandleFunc(string, func(http.ResponseWriter, *http.Request))
	ServeHTTP(http.ResponseWriter, *http.Request)
	Param(*http.Request, string) string
	Register(Chains)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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