Documentation ¶
Index ¶
- Variables
- func AbortWithStatusJSON(c ResponseWriter, err error, fn ...WithData)
- func AddHead() gin.HandlerFunc
- func AuthLevel(level int) gin.HandlerFunc
- func AuthMiddleware(secret string) gin.HandlerFunc
- func CacheControlMaxAge(millisecond int) gin.HandlerFunc
- func EtagHandler() gin.HandlerFunc
- func Fail(c ResponseWriter, err error, fn ...WithData)
- func GetBaseURL(req *http.Request) string
- func GetGroupLevel(c *gin.Context) int8
- func GetRole(c *gin.Context) string
- func GetUID(c *gin.Context) int
- func GetUsername(c *gin.Context) string
- func HanddleJSONErr(err error) error
- func HandlerResponseMsg(resp http.Response) error
- func IPRateLimiter(r rate.Limit, b int) func(ip string) bool
- func IPRateLimiterForGin(r rate.Limit, b int) gin.HandlerFunc
- func Limit(v, min, max int) int
- func Logger(log *slog.Logger, recordBodyFn func(*gin.Context) bool) gin.HandlerFunc
- func Mertics() gin.HandlerFunc
- func Message(err error) string
- func MustTraceID(ctx context.Context) string
- func NewToken(input TokenInput) (string, error)
- func Offset(page, size int) int
- func RateLimiter(r rate.Limit, b int) gin.HandlerFunc
- func RecordResponse() gin.HandlerFunc
- func Recover() gin.HandlerFunc
- func SendChunk(ch <-chan Chunk, c *gin.Context)
- func SendChunkPro(ch <-chan Chunk, c *gin.Context)
- func SendSSE(ch <-chan EventMessage, c *gin.Context)
- func SetRelease()
- func SetTraceID(ctx *gin.Context, id string)
- func SetupMutexProfile(rate int)
- func SetupPProf(r *gin.Engine, ips *[]string)
- func Success(c HTTPContext, bean any)
- func TraceID(ctx context.Context) (string, bool)
- func WarpH[I any, O any](fn func(*gin.Context, *I) (O, error)) gin.HandlerFunc
- type Chunk
- type Claims
- type E
- type Error
- func (e *Error) Details() []string
- func (e Error) Error() string
- func (e *Error) HTTPCode() int
- func (e *Error) Map() map[string]any
- func (e *Error) Message() string
- func (e *Error) Msg(s string) *Error
- func (e *Error) Reason() string
- func (e *Error) With(args ...string) *Error
- func (e *Error) Withf(format string, args ...any) *Error
- type Errorer
- type EtagWriter
- type Event
- type EventMessage
- type HTTPContext
- type PageOutput
- type PagerFilter
- type ResponseMsg
- type ResponseWriter
- type ResponseWriterWrapper
- type SSE
- type ScrollPager
- type TokenInput
- type Validator
- type WithData
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnknown = NewError("UnKnow", "未知错误") ErrBadRequest = NewError("ErrBadRequest", "请求参数有误") ErrDB = NewError("ErrStore", "数据发生错误") ErrServer = NewError("ErrServer", "服务器发生错误") ErrJSON = NewError("ErrUnmarshal", "JSON 编解码出错") ErrNotFound = NewError("ErrNotFound", "资源未找到") ErrUsedLogic = NewError("ErrUsedLogic", "使用逻辑错误") ErrLoginLimiter = NewError("ErrLoginLimiter", "触发登录限制") ErrPermissionDenied = NewError("ErrPermissionDenied", "没有该资源的权限") ErrTimeout = NewError("ErrTimeout", "请求超时") ErrDevice = NewError("ErrDevice", "设备异常") ErrDeviceOffline = NewError("ErrDeviceOffline", "设备离线") ErrAddNewDevice = NewError("ErrAddNewDevice", "请重新添加此设备") // 待删除 )
常用错误
var ( ErrNameOrPasswd = NewError("ErrNameOrPasswd", "用户名或密码错误") ErrCaptchaWrong = NewError("ErrCaptchaWrong", "验证码错误") ErrAccountDisabled = NewError("ErrAccountDisabled", "登录限制") )
业务错误
Functions ¶
func AbortWithStatusJSON ¶
func AbortWithStatusJSON(c ResponseWriter, err error, fn ...WithData)
func AddHead ¶
func AddHead() gin.HandlerFunc
func AuthLevel ¶
func AuthLevel(level int) gin.HandlerFunc
func CacheControlMaxAge ¶ added in v1.0.6
func CacheControlMaxAge(millisecond int) gin.HandlerFunc
WebCache 主要用于缓存静态资源 Cache-Control: max-age=3600 # 缓存1小时 Cache-Control: no-cache # 每次都需要验证 Cache-Control: no-store # 完全不缓存 Cache-Control: private # 只允许浏览器缓存 Cache-Control: public # 允许中间代理缓存
func EtagHandler ¶ added in v1.0.6
func EtagHandler() gin.HandlerFunc
func GetBaseURL ¶ added in v1.0.2
GetBaseURL 提取请求地址 例如 http://127.0.0.1:8080/health 提取出 http://127.0.0.1:8080
func GetGroupLevel ¶
func HanddleJSONErr ¶
func HandlerResponseMsg ¶
HandlerResponseMsg 获取响应的结果
func IPRateLimiter ¶
IPRateLimiter IP 限流器
func IPRateLimiterForGin ¶
func IPRateLimiterForGin(r rate.Limit, b int) gin.HandlerFunc
IPRateLimiter IP 限流器
func MustTraceID ¶
func RecordResponse ¶
func RecordResponse() gin.HandlerFunc
func Recover ¶
func Recover() gin.HandlerFunc
Recover from panics and converts the panic to an error so it is reported in Metrics and handled in Errors.
func SendSSE ¶
func SendSSE(ch <-chan EventMessage, c *gin.Context)
func SetRelease ¶
func SetRelease()
func SetTraceID ¶
func SetupMutexProfile ¶
func SetupMutexProfile(rate int)
func SetupPProf ¶
Types ¶
type Claims ¶
type Claims struct { UID int Username string GroupID int GroupLevel int8 Role string Level int jwt.RegisteredClaims }
Claims ...
type E ¶
type E struct { Reason string `json:"reason"` Msg string `json:"msg"` Details []string `json:"details"` }
E 可反序列化的 err
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error ...
type EtagWriter ¶ added in v1.0.6
type EtagWriter struct { gin.ResponseWriter // contains filtered or unexported fields }
func (*EtagWriter) Unwrap ¶ added in v1.0.6
func (w *EtagWriter) Unwrap() http.ResponseWriter
type EventMessage ¶
type EventMessage struct {
// contains filtered or unexported fields
}
func NewEventMessage ¶
func NewEventMessage(event string, data map[string]any) *EventMessage
type HTTPContext ¶
type PageOutput ¶
PageOutput 分页数据
type PagerFilter ¶
type PagerFilter struct { Page int `form:"page"` Size int `form:"size"` Sort string `form:"sort"` SortSafelist []string `json:"-"` }
PagerFilter 分页过滤
func (PagerFilter) MustSortColumn ¶
func (f PagerFilter) MustSortColumn() string
MustSortColumn 忽略安全问题
func (PagerFilter) SortColumn ¶
func (f PagerFilter) SortColumn() (string, error)
SortColumn 通过对 SortColumn 设置值,仅对允许的值做排序处理
func (PagerFilter) SortDirection ¶
func (f PagerFilter) SortDirection() string
SortDirection 如果 sort 携带负号返回倒序,否则返回正序
type ResponseMsg ¶
type ResponseMsg struct {
Msg string `json:"msg"`
}
type ResponseWriter ¶
type ResponseWriter interface { JSON(code int, obj interface{}) File(filepath string) Set(string, any) context.Context AbortWithStatusJSON(code int, obj interface{}) }
ResponseWriter ...
type ResponseWriterWrapper ¶
type ResponseWriterWrapper struct { gin.ResponseWriter Body *bytes.Buffer // 缓存 }
func (ResponseWriterWrapper) WriteString ¶
func (w ResponseWriterWrapper) WriteString(s string) (int, error)
type SSE ¶ added in v1.0.2
SSE 发送事件
使用案例 http.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) { sse := web.NewSSE(1024, time.Minute) go func(){ for range 3 { sse.Publish(web.Event{ ID: uuid.New().String(), Event: "ping", Data: []byte("pong"), }) time.Sleep(time.Second) } sse.Close() }() sse.ServeHTTP(w, r) })
type ScrollPager ¶
ScrollPager 滚动翻页