middleware

package
v0.0.0-...-da45c02 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2017 License: Apache-2.0 Imports: 1 Imported by: 0

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 New

func New() *Cbping

func (*Cbping) Bootstrap

func (c *Cbping) Bootstrap()

引导初始

func (*Cbping) MuxHandler

func (c *Cbping) MuxHandler(muxHandler http.Handler)

注册原本路由处理器

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))

func (*Cbping) Run

func (c *Cbping) Run(addr string)

运行

func (*Cbping) ServeHTTP

func (c *Cbping) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type MiddleWare

type MiddleWare interface {
	ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

中间件接口

func Wrap

func Wrap(handler http.Handler) MiddleWare

包装标准库的http.Handler

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)

Jump to

Keyboard shortcuts

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