middleware

package
v0.0.0-...-4e4c4f6 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CtxRequestStart = "bw_request_start" // 请求开始时间
	CtxRequestSize  = "bw_request_size"  // 请求大小
	CtxRequestBody  = "bw_request_body"  // 请求Body
)

请求上下文

View Source
const (
	LogRequestMaxSize  = 1024
	LogResponseMaxSize = 1024
)

日志记录请求响应数据大小上限,太长影响性能

View Source
const (
	LogValueDepartment = "bw"
	LogValueVersion    = "bw-go-1.0"
	LogValueExtra      = "{\"app\":\"default\",\"env\":\"default\",\"mode\":\"default\"}"
)

日志字段默认值

View Source
const (
	HttpHeaderTraceId = "Trace-ID"
)

Http Header

Variables

This section is empty.

Functions

func GetCtxParam

func GetCtxParam(c *gin.Context, key string) interface{}

func GetCtxParamInt

func GetCtxParamInt(c *gin.Context, key string) (i int)

func GetCtxParamString

func GetCtxParamString(c *gin.Context, key string) (s string)

func GetCtxParamTime

func GetCtxParamTime(c *gin.Context, key string) (t time.Time)

func HandlerAccessLog

func HandlerAccessLog() gin.HandlerFunc

ngx访问日志中间件

func HandlerBizLog

func HandlerBizLog() gin.HandlerFunc

业务日志中间件

func HandlerInitContext

func HandlerInitContext() gin.HandlerFunc

初始化注入上下文参数 gin.Context.Keys类型是map,不保证线程安全 在多个goroutine共享,存在冲突的风险 https://github.com/gin-gonic/gin/issues/700 如果context需要在多个goroutine共享,使用context.Copy() 【备注】新的版本已通过读写锁 RWMutex 解决了这个问题

func HandlerRecovery

func HandlerRecovery(stack bool) gin.HandlerFunc

捕捉异常自动恢复,请求异常或堆栈状态信息写入日志

func LogAddCustomHeader

func LogAddCustomHeader(header string)

func LogCritical

func LogCritical(ctx context.Context, a interface{})

func LogDebug

func LogDebug(ctx context.Context, a interface{})

func LogError

func LogError(ctx context.Context, a interface{})

func LogInfo

func LogInfo(ctx context.Context, a interface{})

func LogWarn

func LogWarn(ctx context.Context, a interface{})

Types

type HttpClient

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

func NewHttpClient

func NewHttpClient(t time.Duration) *HttpClient

func (*HttpClient) HttpGet

func (cli *HttpClient) HttpGet(c *gin.Context, addr string, query map[string]string, headers map[string]string) (string, error)

func (*HttpClient) HttpOriginPost

func (cli *HttpClient) HttpOriginPost(c *gin.Context, addr string, query map[string]string, headers map[string]string, body string) (string, error)

func (*HttpClient) JsonPost

func (cli *HttpClient) JsonPost(c *gin.Context, addr string, query map[string]string, headers map[string]string, body interface{}) (string, error)

func (*HttpClient) MultipleJsonPost

func (cli *HttpClient) MultipleJsonPost(c *gin.Context, addr string, params map[string]string, headers map[string]string, bodyList map[int]interface{}) (map[int]string, map[int]error)

并发请求

func (*HttpClient) ThirdFormGet

func (cli *HttpClient) ThirdFormGet(c *gin.Context, addr string, query map[string]string, headers map[string]string) (string, error)

func (*HttpClient) ThirdFormPost

func (cli *HttpClient) ThirdFormPost(c *gin.Context, addr string, query map[string]string, headers map[string]string, body map[string]string) (string, error)

type HttpResponse

type HttpResponse struct {
	ErrCode int         `json:"error_code"`
	Message string      `json:"message"`
	Result  interface{} `json:"result"`
}

func NewHttpResponse

func NewHttpResponse(code int, message, result interface{}) *HttpResponse

type LogConfig

type LogConfig struct {
	AppPath string // 业务日志完整路径
	Extra   LogExtra
}

日志配置

type LogExtra

type LogExtra struct {
	AppName   string `json:"app"`
	Env       string `json:"env"`
	Mode      string `json:"mode"`
	Version   string `json:"version"`
	Namespace string `json:"namespace"`
	PodName   string `json:"pod_name"`
	PodIp     string `json:"pod_ip"`
	Uri       string `json:"uri"`
}

日志字段extra

type Logger

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

func NewLog

func NewLog(config *LogConfig) (*Logger, error)

Jump to

Keyboard shortcuts

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