middleware

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StaticRegexp = regexp.MustCompile("/*/*.(js|css|png|jpg|woff|tff|oet)")

Functions

func CacheControl

func CacheControl(filter *regexp.Regexp) gin.HandlerFunc

func ErrorMiddleware

func ErrorMiddleware(logger errLogger) gin.HandlerFunc

ErrorMiddleware 捕获到在http处理时的错误 在 handler 和其他地方如果产生了 error 可直接panic,到这里统一处理,简化 if err != nil 之类的代码 panic("text") => {msg: "text", code: 0, data: {}} panic(gin.H{"code": 0, "msg": "some error"}) => {与传入的数据一致,} code 默认999999,status 默认 400,msg 默认 unknow error panic(errors.New("some error")) => {msg: "some error", code: 999999, data: {}} panic(Struct{...}) => {msg: "unknow", code: 999999, data: {...struct 数据}}

func Ginzap

func Ginzap(logger *zap.Logger, printBody bool, filter *regexp.Regexp) gin.HandlerFunc

Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

It receives:

  1. A time package format string (e.g. time.RFC3339).
  2. A boolean stating whether to use UTC time zone or local.

func RecoveryWithZap

func RecoveryWithZap(logger *zap.Logger, stack bool) gin.HandlerFunc

RecoveryWithZap returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

Types

This section is empty.

Jump to

Keyboard shortcuts

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