Skip to content
/ chain Public

A simple HTTP middleware chain implement. 一个简单的HTTP中间件chain实现。

License

Notifications You must be signed in to change notification settings

gookit/chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

middleware chain

GoDoc Build Status Coverage Status Go Report Card

A simple http middleware chain implement.

Godoc

Usage

package main

import (
	"github.com/gookit/chain"
	"net/http"
	"testing"
)

func main() {
	middleware0 := func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("a"))
			h.ServeHTTP(w, r)
			w.Write([]byte("A"))
		})
	}
	middleware1 := func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("b"))
			h.ServeHTTP(w, r)
			w.Write([]byte("B"))
		})
	}

	c := chain.New(middleware0, middleware1)

	c.Use(func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("c"))
			h.ServeHTTP(w, r)
			w.Write([]byte("C"))
		})
	})

	h := c.Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("-(CORE)-"))
		w.WriteHeader(200)
	}))
 
	http.ListenAndServe(":8090", h)
	// Output: abc-(CORE)-CBA
}

License

MIT

About

A simple HTTP middleware chain implement. 一个简单的HTTP中间件chain实现。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages