github.com/anjalikarhana/fabric@v2.1.1+incompatible/core/middleware/chain.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package middleware 8 9 import ( 10 "net/http" 11 ) 12 13 type Middleware func(http.Handler) http.Handler 14 15 // A Chain is a middleware chain use for http request processing. 16 type Chain struct { 17 mw []Middleware 18 } 19 20 // NewChain creates a new Middleware chain. The chain will call the Middleware 21 // in the order provided. 22 func NewChain(middlewares ...Middleware) Chain { 23 return Chain{ 24 mw: append([]Middleware{}, middlewares...), 25 } 26 } 27 28 // Handler returns an http.Handler for this chain. 29 func (c Chain) Handler(h http.Handler) http.Handler { 30 if h == nil { 31 h = http.DefaultServeMux 32 } 33 34 for i := len(c.mw) - 1; i >= 0; i-- { 35 h = c.mw[i](h) 36 } 37 return h 38 }