Skip to Content
Mino

chain · Middleware

Compose and reuse standard HTTP middleware independently.

简体中文 ↗

Middleware uses func(http.Handler) http.Handler and can wrap any http.Handler.

Compose middleware

main.go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/hainuoli/mino/rest/chain"
)

func main() {
	handler := chain.New(serviceHeader).ThenFunc(hello)
	log.Fatal(http.ListenAndServe(":8080", handler))
}

func serviceHeader(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("X-Service", "Mino")
		next.ServeHTTP(w, r)
	})
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello, Mino!")
}

Complete example · cd examples && go run ./packages/chain

curl -i http://localhost:8080/

The response includes X-Service: Mino and a Hello, Mino! body.

Execution order

chain.New(first, second, third).Then(handler)
first → second → third → handler

Requests enter in registration order. After next.ServeHTTP returns, middleware unwinds in reverse order.

Reuse and extend

base := chain.New(serviceHeader)

public := base.ThenFunc(hello)
private := base.Append(auth).ThenFunc(hello)

Append, Prepend, and Extend return new chains without changing base.

MethodPosition
Append(mw...)After the existing middleware
Prepend(mw...)Before the existing middleware
Extend(other)Append another chain

Wrap an existing router

mux := http.NewServeMux()
mux.HandleFunc("GET /", hello)

handler := chain.New(serviceHeader).Then(mux)

Mino, Chi, and other routers implementing http.Handler work the same way.

Conventions

  • The zero value is a usable empty chain.
  • Then takes an http.Handler; ThenFunc takes an http.HandlerFunc.
  • Then(nil) and ThenFunc(nil) select http.DefaultServeMux.
  • Middleware is composed when the handler is built. Returning a nil handler panics.

API · Packages and defaults

On this page