misc

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, MIT Imports: 3 Imported by: 1

README

Misc 常用小中间组件

定义了以系列的常用小中间件

组件列表

  • ElapsedTime 请求执行时间中间件。在返回头里加入请求的执行时间
  • Headers 响应头中间件。在返回头内加入固定的响应头
  • 逻辑判断中间件,根据给定的参数或者判断函数

使用方式

ElapsedTime

使用该组件后,程序执行时间会加在响应头的 "Elapsed-Time" 中

app.Use(ElapsedTime)
Headers

配置方式:

#TOML版本,其他版本可以根据对应格式配置
"Header1"=["key1"]
"Header2"=["key2","key3"]

使用: m:=&misc.Headers{} err=toml.Unmarshal(data,m) app.Use(m)

逻辑判断中间件

静态判断

app.Use(
    //启动时条件为真执行动作(http.HandlerFunc)
    misc.If(true,action1),
    //启动时条件为真返回指定状态码的错误
    misc.ErrorIf(true,404),
    //启动时条件为真加入指定的中间件
    misc.MiddlewareIf(true,middleware1)
)

动态判断

//创建条件判断函数
condition=func() (bool, error){
    return time.Now().Before(starttime),nil
}

app.Use(
    //运行时条件为真执行动作(http.HandlerFunc)
    misc.When(condition,action1),
    //运行时条件为真返回指定状态码的错误
    misc.ErrorWhen(condition,404),
    //运行时条件为真加入指定的中间件
    misc.MiddlewareWhen(condition,middleware1)
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ElapsedTime

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

ElapsedTime add requset elapsed time to "Elapsed-Time" header of response. Elapsed time is time spent between middleware exetue and data wrote to response.

func ErrorIf

func ErrorIf(condition bool, status int) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ErrorIf middleware return http error if condition is true.

func ErrorWhen

func ErrorWhen(condition func() (bool, error), status int) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ErrorWhen middleware return http error if result of condition function is true. Panic if any error raised.

func If

func If(condition bool, then http.HandlerFunc) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

If middleware checks condition. If condition is true,params 'then' will be executed. Only checks condition when init.

func MiddlewareIf

func MiddlewareIf(condition bool, then func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

MiddlewareIf return middleware after condition checked. If condition is true,mirddleware 'then' will be returned. Only checks condition when init.

func MiddlewareWhen

func MiddlewareWhen(condition func() (bool, error), then func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

MiddlewareWhen return middleware after condition checked. If result of condition function is true,params 'then' will be executed. Panic if any error raised. Checks condition every time.

func When

func When(condition func() (bool, error), then http.HandlerFunc) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

When middleware checks condition. If result of condition function is true,params 'then' will be executed. Panic if any error raised. Checks condition every time.

Types

type Headers

type Headers map[string]string

Headers headers middleware which add headers to each response

func (*Headers) ServeMiddleware

func (h *Headers) ServeMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

ServeMiddleware serve headers settings as middleware

type Writer

type Writer interface {
	http.ResponseWriter
	http.Hijacker
}

Writer http response writer interface.

Jump to

Keyboard shortcuts

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