Documentation
¶
Index ¶
- 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 CountGoroutines(d time.Duration, num uint8)
- 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 GetHost(c *http.Request) string
- 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 Metrics() gin.HandlerFunc
- 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 SetDebug()
- 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 DateFilter
- type EtagWriter
- type Event
- type EventMessage
- type GoroutineNum
- 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 ¶
This section is empty.
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 ¶
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 CountGoroutines ¶
CountGoroutines 协程数量,间隔 duration 记录一次
func EtagHandler ¶
func EtagHandler() gin.HandlerFunc
func GetBaseURL ¶
GetBaseURL 提取请求地址 例如 http://127.0.0.1:8080/health 提取出 http://127.0.0.1:8080
func GetGroupLevel ¶
func GetHost ¶
GetHost 提取主机 IP 或域名 例如 http://127.0.0.1:8080/health 提取出 127.0.0.1
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 DateFilter ¶
DateFilter 日期区间过滤
func (DateFilter) DefaultEndAt ¶
func (d DateFilter) DefaultEndAt(date time.Time) time.Time
DefaultEndAt 当为零值或不符合规则时,返回提供的默认值
func (DateFilter) DefaultStartAt ¶
func (d DateFilter) DefaultStartAt(date time.Time) time.Time
DefaultStartAt 当为零值或不符合规则时,返回提供的默认值
type EtagWriter ¶
type EtagWriter struct {
gin.ResponseWriter
// contains filtered or unexported fields
}
func (*EtagWriter) Unwrap ¶
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 GoroutineNum ¶
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 NewPagerFilterMaxSize ¶
func NewPagerFilterMaxSize() 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 ¶
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 滚动翻页