Documentation ¶
Overview ¶
把中间件和原本的路由处理器封装在一起, 先执行中间件,如果中间件没有提前结束请求, 最终会把执行权归还给原本的路由处理器。 中间件允许注册多个,执行顺序和注册顺序一致。 其实原本的路由处理器也可以看做一个中间件了,不过,它是放在最后一个执行位置上(除了末尾的空中间件)。 参考开源项目:https://github.com/urfave/negroni
http.HandleFunc("/log", func) mw := middleware.New() mw.RegisterMiddlewareHandleFunc(Recovery, Token) mw.Run(":9999" )
OR
package main import ( "fmt" "net/http" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "Welcome to the home page!") }) n := middleware.New() n.MuxHandler(mux) n.RegisterMiddlewareHandleFunc(Middleware1,Middleware2) n.Bootstrap() http.ListenAndServe(":3000", n) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cbping ¶
type Cbping struct {
// contains filtered or unexported fields
}
Cbping是一堆中间件处理程序管理器, 可以当作http.handler被调用 通过RegisterMiddlewareHandleFunc|RegisterMiddleWare注册中间件
func (*Cbping) RegisterMiddleWare ¶
func (c *Cbping) RegisterMiddleWare(handler MiddleWare)
注册中间件 中间件执行顺序和注册顺序一致
func (*Cbping) RegisterMiddlewareHandleFunc ¶
func (c *Cbping) RegisterMiddlewareHandleFunc(handlers ...func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc))
type MiddleWare ¶
type MiddleWare interface {
ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}
中间件接口
type MiddleWareFunc ¶
type MiddleWareFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
func (MiddleWareFunc) ServeHTTP ¶
func (h MiddleWareFunc) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
Click to show internal directories.
Click to hide internal directories.