wrap

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package wrap provides a way to create middleware chains for HTTP handlers in Go.

The wrap methods and Chain are essentially the same as the `github.com/justinas/alice` package, which allows for chaining middleware in a clean and reusable way. All credit goes to `github.com/justinas`.

I just reimplemented it with a new name to match my semantic flow needs, and added some additional functionality to it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(h http.Handler, middleware ...Middleware) http.Handler

After wraps a handler with the provided middleware in reverse order. It returns the resulting http.Handler. This is useful for applying middleware that should run after the main handler

Example: h := wrap.After(myHandler, middleware1, middleware2) // h is now wrapped with middleware2 and middleware1, reversed

func Around

func Around(h http.Handler, middleware ...Middleware) http.Handler

Around wraps a handler with the provided middleware in the order they are passed It return the resulting http.Handler. So, it's mostly useful for on-the-fly middleware application.

Example: h := wrap.Around(myHandler, middleware1, middleware2) // h is now wrapped with middleware1 and middleware2

func Before

func Before(h http.Handler, middleware ...Middleware) http.Handler

Before is an alias for Around to provide a more semantic API

Types

type Chain

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

Chain represents an immutable chain of http.Handler middleware

func New

func New(middleware ...Middleware) Chain

New creates a new middleware chain, memoizing the middlewares

func (Chain) Append

func (c Chain) Append(middleware ...Middleware) Chain

Append adds additional middleware to the chain and returns a new chain 1. It returns a new chain with the combined middlewares 2. The original chain remains unchanged 3. This allows for composing middleware chains easily

Example: chain := wrap.New(middleware1).Append(middleware2, middleware3)

func (Chain) Extend

func (c Chain) Extend(chain Chain) Chain

func (Chain) Then

func (c Chain) Then(h http.Handler) http.Handler

Then chains the middleware to the given http.Handler and returns the resulting http.Handler Chains can be safely reused, and the original chain remains unchanged.

Example:

chain := wrap.New(middleware1, middleware2) pipe1 := chain.Then(anotherHandler) pipe2 := chain.Then(yetAnotherHandler)

func (Chain) ThenFunc

func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler

ThenFunc wraps the given handler function with all middleware in the chain and returns the resulting http.Handler. If the provided handler function is nil, it returns a handler that does nothing.

Example: chain := wrap.New(middleware1, middleware2) pipe1 := chain.ThenFunc(myHandlerFunc) pipe2 : = chain.ThenFunc(anotherHandlerFunc)

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents a function that wraps an http.Handler with additional functionality

Jump to

Keyboard shortcuts

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