ginserver

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// 请求参数绑定错误 回复状态:http.StatusBadRequest
	JsonParamBindError = map[string]interface{}{"errCode": 1, "errDesc": "Param Error"}
	// 处理逻辑Panic了 回复状态:http.StatusInternalServerError
	PanicError = map[string]interface{}{"errCode": 500, "errDesc": "Server Error"}
	// 生成Context
	Context = func(parent context.Context, c *gin.Context) context.Context {
		return context.WithValue(parent, utils.CtxKey_traceId, utils.GenTraceID())
	}
)

外部可赋值重定义

Functions

This section is empty.

Types

type CorsConfig

type CorsConfig struct {
	AllowOrigin      []string `json:"alloworigin,omitempty"`      // Access-Control-Allow-Origin:
	AllowMethods     []string `json:"allowmethods,omitempty"`     // Access-Control-Allow-Methods
	AllowHeaders     []string `json:"allowheaders,omitempty"`     // Access-Control-Allow-Headers
	MaxAge           int      `json:"maxage,omitempty"`           // Access-Control-Max-Age 秒
	AllowCredentials bool     `json:"allowcredentials,omitempty"` // Access-Control-Allow-Credentials
}

跨域控制配置 参考 https://github.com/rs/cors/

func (*CorsConfig) Normalize

func (c *CorsConfig) Normalize()

type GinServer

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

func NewGinServer

func NewGinServer(port int) *GinServer

func (*GinServer) Engine

func (gs *GinServer) Engine() *gin.Engine

Engine 暴露原始对象

func (*GinServer) RegHandler

func (gs *GinServer) RegHandler(method, path string, fun interface{}, optionsHandlers ...gin.HandlerFunc)

注册回调,需要fun调用回复 method为空表示注册所有方法 fun的参数支持以下写法 (c *gin.Context) (ctx context.Context, c *gin.Context) optionsHandlers会先执行 再执行fun调用

func (*GinServer) RegHook

func (gs *GinServer) RegHook(f func(ctx context.Context, c *gin.Context, elapsed time.Duration))

func (*GinServer) RegJsonHandler

func (gs *GinServer) RegJsonHandler(method, path string, fun interface{}, optionsHandlers ...gin.HandlerFunc)

注册Json数据结构的回调,不需要fun调用回复 method为空表示注册所有方法 fun的参数支持以下写法 (ctx context.Context, c *gin.Context, resp *RespStruct) (ctx context.Context, c *gin.Context, req *ReqStruct, resp *RespStruct) ReqStruct结构的格式可以是gin支持绑定的任意格式,需要在请求头中Content-Type指定具体格式,然后ReqStruct对应的写tag即可 RespStruct必须是支持json格式化的结构 optionsHandlers会先执行 再执行fun调用

func (*GinServer) RegNoRouteHandler

func (gs *GinServer) RegNoRouteHandler(fun interface{}, optionsHandlers ...gin.HandlerFunc)

注册无路由的处理逻辑 fun的参数支持以下写法 (ctx context.Context, c *gin.Context) optionsHandlers会先执行 再执行fun调用

func (*GinServer) Start

func (gs *GinServer) Start() error

func (*GinServer) StartTLS

func (gs *GinServer) StartTLS(certFile, keyFile string) error

func (*GinServer) Stop

func (gs *GinServer) Stop() error

type ParamConfig

type ParamConfig struct {
	// 日志级别和zerolog.Level一致
	// IP和Path共存的配置,优先使用Path配置
	LogLevel           int             `json:"loglevel,omitempty"`           // 默认的级别,不配置就是info级别
	LogLevelByIP       map[string]int  `json:"loglevelbyip,omitempty"`       // 根据IP区分的日志级别,IP:日志级别, IP支持?*通配符 不区分大小写,不配置默认使用LogLevel级别
	LogLevelByPath     map[string]int  `json:"loglevelbypath,omitempty"`     // 根据Path区分的日志级别,Path:日志级别 Path支持?*通配符 不区分大小写,不配置默认使用LogLevel级别
	LogLevelHead       int             `json:"loglevelhead,omitempty"`       // 请求头和回复头的日志级别,不配置就是debug级别
	LogLevelHeadByIP   map[string]int  `json:"loglevelheadbyip,omitempty"`   // 根据IP区分的请求头和回复头日志级别,IP:日志级别, IP支持?*通配符 不区分大小写,不配置默认使用LogLevelHead级别
	LogLevelHeadByPath map[string]int  `json:"loglevelheadbypath,omitempty"` // 根据Path区分的请求头和回复头日志级别,Path:日志级别 Path支持?*通配符 不区分大小写,不配置默认使用LogLevelHead级别
	LogMerge           bool            `json:"logmerge,omitempty"`           // req和resp是否合并成一条日志 默认合并
	LogMergeByIP       map[string]bool `json:"logmergebyip,omitempty"`       // 根据IP区分req和resp是否合并一条日志,IP:是否合并, IP支持?*通配符 不区分大小写,不配置默认使用LogMerge配置
	LogMergeByPath     map[string]bool `json:"logmergebypath,omitempty"`     // 根据Path区分req和resp是否合并一条日志,Path:是否合并 Path支持?*通配符 不区分大小写,不配置默认使用LogMerge配置

	TimeOutCheck int `json:"timeoutcheck,omitempty"` // 消息超时监控 单位秒 默认0不开启监控

	Cors         *CorsConfig `json:"cors,omitempty"`
	BodyLogLimit int         `json:"bodyloglimit,omitempty"` // body日志限制 <=0 表示不限制
	// Timeout: 执行 command 的超时时间 单位为毫秒
	// MaxConcurrentRequests: 最大并发量
	// RequestVolumeThreshold: 一个统计窗口 10 秒内请求数量 达到这个请求数量后才去判断是否要开启熔断
	// SleepWindow: 熔断器被打开后 SleepWindow的时间就是控制过多久后去尝试服务是否可用了 单位为毫秒
	// ErrorPercentThreshold: 错误百分比 请求数量大于等于 RequestVolumeThreshold 并且错误率到达这个百分比后就会启动熔断
	Hystrix map[string]*hystrix.CommandConfig `json:"hystrix,omitempty"` // 熔断器 [path:Config],path 支持?*通配符 不区分大小写 目前不支持动态删除
}

参数配置

func (*ParamConfig) Create

func (c *ParamConfig) Create()

func (*ParamConfig) GetLogLevel

func (c *ParamConfig) GetLogLevel(path, ip string) int

func (*ParamConfig) GetLogLevelHead

func (c *ParamConfig) GetLogLevelHead(path, ip string) int

func (*ParamConfig) GetLogMerge

func (c *ParamConfig) GetLogMerge(path, ip string) bool

func (*ParamConfig) IsHystrixPath

func (c *ParamConfig) IsHystrixPath(path string) (string, bool)

func (*ParamConfig) Normalize

func (c *ParamConfig) Normalize()

Jump to

Keyboard shortcuts

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