httpchain

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 2 Imported by: 1

README

httpchain

GoDoc

Package httpchain

this package provide Chain function to chain multiple middleware into single handler function

Chain function

Chain multiple middleware into single handler function

Middleware is any value that have following type

func(next http.HandlerFunc) http.HandlerFunc

you can pass multiple middleware, slice/array of middleware, or combination of them

this function also accept following type as handler (last function in middleware chain)

http.HandlerFunc
http.Handler
func(*http.Request) http.HandlerFunc

(the last one is from go-handler package)

when you have following code

var h http.HandlerFunc
var m func(http.HandlerFunc) http.HandlerFunc
var ms [2]func(http.HandlerFunc) http.HandlerFunc

then

all := Chain(m, ms, h)

will have same effect as

all := m(ms[0](ms[1](h)))

Documentation

Overview

Package httpchain

this package provide Chain function to chain multiple middleware into single handler function

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain(all ...any) http.HandlerFunc

Chain multiple middleware into single handler function

Middleware is any value that have following type

func(next http.HandlerFunc) http.HandlerFunc
func(next http.Handler) http.Handler
func(next http.HandlerFunc) http.Handler
func(next http.Handler) http.HandlerFunc

you can pass multiple middleware, slice/array of middlewares, or combination of them

this function also accept following type as handler (last function in middleware chain)

http.HandlerFunc
http.Handler
func(*http.Request) http.HandlerFunc
func(http.ResponseWriter, *http.Request) error

when you have following code

var h http.HandlerFunc
var m func(http.HandlerFunc) http.HandlerFunc
var ms [2]func(http.HandlerFunc) http.HandlerFunc

then

all := Chain(m, ms, h)

will have same effect as

all := m(ms[0](ms[1](h)))
Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/payfazz/httpchain"
)

func main() {
	middleware1 := func(next http.HandlerFunc) http.HandlerFunc {
		return func(w http.ResponseWriter, r *http.Request) {
			w.Header().Add("Before", "m1")
			next(w, r)
			w.Header().Add("After", "m1")
		}
	}

	middleware2 := func(next http.HandlerFunc) http.HandlerFunc {
		return func(w http.ResponseWriter, r *http.Request) {
			w.Header().Add("Before", "m2")
			next(w, r)
			w.Header().Add("After", "m2")
		}
	}

	handler := func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "x")
	}

	all := httpchain.Chain(
		middleware1,
		middleware2,
		handler,
	)

	res := httptest.NewRecorder()
	req := httptest.NewRequest("GET", "/", nil)
	all(res, req)

	fmt.Printf(
		"%s,%s,%s,%s,%s\n",
		res.Header().Values("Before")[0],
		res.Header().Values("Before")[1],
		res.Body.String(),
		res.Header().Values("After")[0],
		res.Header().Values("After")[1],
	)

}
Output:

m1,m2,x,m2,m1

Types

This section is empty.

Jump to

Keyboard shortcuts

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