middleware

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 2 Imported by: 3

README

Middleware 中间件组件

提供一系列中间件操作工具和常用中间件,包括:

洋葱模型

中间件实现了洋葱模型。

所有的请求会一次执行next前的部分,然后执行后响应代码会反向执行next之后的部分。

app.
    Use(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc){
        //执行顺序1
        DoSomethingBeforeHanlderA()

        //代码执行挂起,运行下一个中间件
         next(w,r)

        //执行顺序5
        DoSomethingAfterHandlerE()
    }).
    Use(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc){
        //执行顺序2
        DoSomethingBeforeHanlderB()

        //代码执行挂起,运行下一个中间件
         next(w,r)

        //执行顺序4
        DoSomethingAfterHandlerD()

    }).HanldeFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc){
        //执行代码处理部分
        //执行顺序3
        DoSomethingC()

        //执行完毕,开始反向执行中间件next之后部分
    })

如果代码中有Panic,会直接跳过所有之后的请求部分和响应部分的代码

使用方法

使用app对象

本组件提供了一个用于进行中间件操作的类 App,可以用来组合一系列的中间件以供使用

//创建空app
app=middlewre.New()
//根据现有中间件创建app
app2=middlewre.New(middleware1,middleware2,middleware3)
app.
    //按顺序引入中间件
    Use(middleware1,middleware2,middleware3)
    //按顺序引入实现了HandlerSlice的类
    .Chain(hanlder1,handler2)
    //按顺序引入其他app
    .UseApp(app2,app3,app4)
    //最终执行http.HanlderFunc
    .HanldeFunc(httphanlderfunc)    

//最终执行http.Hanlder
app2.Hanlde(httphanler)
转换中间件
//将http.Hanlder转换为中间件
//将执行http.Hanlder的ServerHTTP,再调用下一中间件
middlewareHttpHanlder:=Wrap(httphanler)

//将http.HanlderFunc转换为中间件
//将先执行http.HanlderFunc,再调用下一个中间件

//将app转化为中间件
miedlewareApp:=AppToMiddlewares(app,app2,app3)

子组件列表

  • 用于快速管理中间件的 Middleware与App对象
  • Basicauth HTTPBasic验证
  • Csrf Csrf验证组件
  • Errorpage 自定义错误页中间件
  • Forwarded 被请求转发的信息管理组件
  • Misc 杂项中间件
  • Router 路由

Documentation

Overview

Package middleware provide a app interface to use middleware easily. All middleware is func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppToMiddlewares

func AppToMiddlewares(app ...HandlerSlice) []func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

AppToMiddlewares : Convert App to slice of middleware.

func Chain

func Chain(dst HandlerChain, src ...HandlerSlice)

Chain : Append All middlewares in src to dst. Dst will not change when new middleware appended to src.

func ServeHTTP

func ServeHTTP(app HandlerSlice, w http.ResponseWriter, r *http.Request)

ServeHTTP : Server app as http.

func ServeMiddleware

func ServeMiddleware(app HandlerSlice, w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ServeMiddleware : Server app as middleware.

func Use

func Use(app HandlerChain, middlewares ...func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))

Use : Append middlewares tp app.

func Wrap

func Wrap(f http.Handler) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Wrap : Wrap http.Handler to middleware. Next will be called after handlerFunc finish .

func WrapFunc

func WrapFunc(handlerFunc http.HandlerFunc) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

WrapFunc : Wrap http.HandlerFunc [func(w http.ResponseWriter, r *http.Request)] to middleware. Next will be called after handlerFunc finish .

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App : Slice of middlewares with tons of helpful method.

func New

func New(funcs ...func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) *App

New : Create new chainable middleware app.

func (*App) Chain

func (a *App) Chain(src ...HandlerSlice) *App

Chain : Append All middlewares in src to app. App will not change when new middleware appended to src.

func (*App) Handle

func (a *App) Handle(Handler http.Handler) *App

Handle : Use http Handler as last middleware.

func (*App) HandleFunc

func (a *App) HandleFunc(HandlerFunc http.HandlerFunc) *App

HandleFunc : Use http HandlerFunc as last middleware.

func (*App) Handlers

func (a *App) Handlers() []func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Handlers : Return all middlewares in app.

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP : Use app as a http handler.

func (*App) ServeMiddleware

func (a *App) ServeMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ServeMiddleware : Use app as a middleware.

func (*App) SetHandlers

func (a *App) SetHandlers(h []func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))

SetHandlers : Set app's middlewares.

func (*App) Use

func (a *App) Use(middlewares ...func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) *App

Use : Append middlewares to app.

func (*App) UseApp

func (a *App) UseApp(apps ...*App) *App

UseApp : Append apps as middlewares to app. App will change when new middleware appended to apps.

type HandlerChain

type HandlerChain interface {
	HandlerSlice
	SetHandlers([]func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))
}

HandlerChain : interface which contains slice of middlewares which can be updated.

type HandlerSlice

type HandlerSlice interface {
	Handlers() []func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

HandlerSlice : interface which contains slice of middlewares.

type Middleware

type Middleware func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Middleware middleware interface.

type Middlewares

type Middlewares []func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Middlewares middleware list interaface.

Directories

Path Synopsis
Package csrf provide csrf prevent middleware.
Package csrf provide csrf prevent middleware.

Jump to

Keyboard shortcuts

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