core

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableRecordMetrics

func DisableRecordMetrics(ctx Context)

DisableRecordMetrics 禁止记录指标

func DisableTraceLog

func DisableTraceLog(ctx Context)

DisableTraceLog 禁止记录日志

func NewMux

func NewMux() *mux

func ReleaseContext

func ReleaseContext(ctx Context)

Types

type BusinessError

type BusinessError interface {

	// WithError 设置错误信息
	WithError(err error) BusinessError

	// WithAlert 设置告警通知
	WithAlert() BusinessError

	// BusinessCode 获取业务码
	BusinessCode() int

	// HTTPCode 获取 HTTP 状态码
	HTTPCode() int

	// Message 获取错误描述
	Message() string

	// StackError 获取带堆栈的错误信息
	StackError() error

	// IsAlert 是否开启告警通知
	IsAlert() bool
	// contains filtered or unexported methods
}

func BadRequest

func BadRequest(businessCode int) BusinessError

400

func BadRequestError

func BadRequestError(businessCode int, message string) BusinessError

func Error

func Error(httpCode, businessCode int, message string) BusinessError

func ForbiddenError

func ForbiddenError(businessCode int, message string) BusinessError

403

func InternalServerError

func InternalServerError(businessCode int, message string) BusinessError

500

func NotFoundError

func NotFoundError(businessCode int, message string) BusinessError

404

func NotImplementedError

func NotImplementedError(businessCode int, message string) BusinessError

501

func Unauthorized

func Unauthorized(businessCode int) BusinessError

401

func UnauthorizedError

func UnauthorizedError(businessCode int, message string) BusinessError

type Context

type Context interface {
	Init()

	// ShouldBindQuery 反序列化 querystring
	// tag: `form:"xxx"` (注:不要写成 query)
	ShouldBindQuery(obj interface{}) error

	// ShouldBindPostForm 反序列化 postform (querystring会被忽略)
	// tag: `form:"xxx"`
	ShouldBindPostForm(obj interface{}) error

	// ShouldBindForm 同时反序列化 querystring 和 postform;
	// 当 querystring 和 postform 存在相同字段时,postform 优先使用。
	// tag: `form:"xxx"`
	ShouldBindForm(obj interface{}) error

	// ShouldBindJSON 反序列化 postjson
	// tag: `json:"xxx"`
	ShouldBindJSON(obj interface{}) error

	// ShouldBindURI 反序列化 path 参数(如路由路径为 /user/:name)
	// tag: `uri:"xxx"`
	ShouldBindURI(obj interface{}) error

	// Redirect 重定向
	Redirect(code int, location string)

	// Trace 获取 Trace 对象
	Trace() Trace
	SetTrace(trace Trace)

	// Logger 获取 Logger 对象
	Logger() *zap.Logger
	SetLogger(logger *zap.Logger)

	// Payload 正确返回
	Payload(payload interface{})
	GetPayload() interface{}

	// HTML 返回界面
	HTML(name string, obj interface{})

	// AbortWithError 错误返回
	AbortWithError(err BusinessError)
	AbortError() BusinessError

	Abort()
	JSON(code int, data any)

	// Header 获取 Header 对象
	Header() http.Header
	// GetHeader 获取 Header
	GetHeader(key string) string
	// SetHeader 设置 Header
	SetHeader(key, value string)

	// GetCookie 获取 cookie
	GetCookie(key string) string
	// SetCookie 设置 cookie
	SetCookie(key, value string, maxAge int, path string, host string)
	Get(key string) (any, bool)
	Set(key string, value any)
	Next()
	UserAgent() string

	// SessionUser 当前用户信息
	SessionUser() (interface{}, error)
	SetSessionUser(info interface{})

	// SessionUserErr 当前用户error信息
	SessionUserErr() *string
	SetSessionUserErr(err *string)

	Param(key string) string
	Query(key string) string

	// Alias 设置路由别名 for metrics path
	Alias() string

	AbleRecordMetrics()
	IsRecordMetrics() bool

	// RequestInputParams 获取所有参数
	RequestInputParams() url.Values
	// RequestPostFormParams  获取 PostForm 参数
	RequestPostFormParams() url.Values
	// Request 获取 Request 对象
	Request() *http.Request
	// RawData 获取 Request.Body
	RawData() []byte
	// Method 获取 Request.Method
	Method() string
	// Host 获取 Request.Host
	Host() string
	// Path 获取 请求的路径 Request.URL.Path (不附带 querystring)
	Path() string
	// URI 获取 unescape 后的 Request.URL.RequestURI()
	URI() string
	// RequestContext 获取请求的 context (当 client 关闭后,会自动 canceled)
	RequestContext() StdContext

	// ResponseWriter 获取 ResponseWriter 对象
	ResponseWriter() gin.ResponseWriter
	// contains filtered or unexported methods
}

func NewContext

func NewContext(ctx *gin.Context) Context

type HandlerFunc

type HandlerFunc func(c Context)

func AliasForRecordMetrics

func AliasForRecordMetrics(path string) HandlerFunc

AliasForRecordMetrics 对请求路径起个别名,用于记录指标。 如:Get /user/:username 这样的路径,因为 username 会有非常多的情况,这样记录指标非常不友好。

func WrapAuthHandler

func WrapAuthHandler(handler func(Context) (sessionUser interface{}, err BusinessError)) HandlerFunc

WrapAuthHandler 用来处理 Auth 的入口

type IRoutes

type IRoutes interface {
	Any(string, ...HandlerFunc)
	GET(string, ...HandlerFunc)
	POST(string, ...HandlerFunc)
	DELETE(string, ...HandlerFunc)
	PATCH(string, ...HandlerFunc)
	PUT(string, ...HandlerFunc)
	OPTIONS(string, ...HandlerFunc)
	HEAD(string, ...HandlerFunc)
}

IRoutes 包装gin的IRoutes

type Mux

type Mux interface {
	http.Handler
	Group(relativePath string, handlers ...HandlerFunc) RouterGroup
}

Mux http mux

func New

func New(logger *zap.Logger, options ...Option) (Mux, error)

type Option

type Option func(*option)

func WithAlertNotify

func WithAlertNotify(notifyHandler alert.NotifyHandler) Option

WithAlertNotify 设置告警通知

func WithDisablePProf

func WithDisablePProf() Option

WithDisablePProf 禁用 pprof

func WithDisablePrometheus

func WithDisablePrometheus() Option

WithDisablePrometheus 禁用prometheus

func WithDisableSwagger

func WithDisableSwagger() Option

WithDisableSwagger 禁用 swagger

func WithEnableCors

func WithEnableCors() Option

WithEnableCors 设置支持跨域

func WithEnableGZip

func WithEnableGZip() Option

WithEnableGZip 设置支持gzip

func WithEnableOpenBrowser

func WithEnableOpenBrowser(uri string) Option

WithEnableOpenBrowser 启动后在浏览器中打开 uri

func WithEnableRate

func WithEnableRate() Option

WithEnableRate 设置支持限流

func WithMaxRequestsPerSecond

func WithMaxRequestsPerSecond(maxRequestsPerSecond int) Option

WithMaxRequestsPerSecond

func WithMiddlewares

func WithMiddlewares(middlewares []gin.HandlerFunc, replaceAll bool) Option

func WithRateRouter

func WithRateRouter(cb rateRouter) Option

func WithRecordMetrics

func WithRecordMetrics(recordHandler alert.RecordHandler) Option

WithRecordMetrics 设置记录接口指标

func WithRegisterRouter

func WithRegisterRouter(cb registerRouter) Option

func WithTraceHeaders

func WithTraceHeaders(headers []string) Option

WithTraceHeaders

func WithTraceRouter

func WithTraceRouter(cb traceRouter) Option

func WithWithoutTracePaths

func WithWithoutTracePaths(paths map[string]bool, replaceAll bool) Option

WithWithoutTracePaths

type RouterGroup

type RouterGroup interface {
	Group(string, ...HandlerFunc) RouterGroup
	IRoutes
}

RouterGroup 包装gin的RouterGroup

type StdContext

type StdContext struct {
	stdctx.Context
	Trace
	*zap.Logger
}

type Trace

type Trace = trace.T

type TraceOptions

type TraceOptions struct {
	AlertNotify       alert.NotifyHandler
	RecordHandler     alert.RecordHandler
	TraceHeaders      []string
	WithoutTracePaths map[string]bool // 这些请求,默认不记录日志
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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