Documentation ¶
Overview ¶
This package provides Sentinel middleware for fiber.
Example ¶
app := fiber.New() app.Use( SentinelMiddleware( // customize resource extractor if required // method_path by default WithResourceExtractor(func(ctx *fiber.Ctx) string { headers := ctx.GetReqHeaders() ipValues, exists := headers["X-Real-IP"] var ip string if exists && len(ipValues) > 0 { ip = ipValues[0] // 获取第一个IP值 } else { // 如果不存在或没有值,则可以设置默认值或者处理逻辑 ip = "default-ip" // 示例中的默认值 } return ip }), // customize block fallback if required // abort with status 429 by default WithBlockFallback(func(ctx *fiber.Ctx) error { return ctx.Status(400).JSON(struct { Error string `json:"error"` Code int `json:"code"` }{ "too many request; the quota used up", 10222, }) })), ) app.Get("/test", func(ctx *fiber.Ctx) error { return nil }) _ = app.Listen(":8080")
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SentinelMiddleware ¶
func SentinelMiddleware(opts ...Option) fiber.Handler
SentinelMiddleware returns new gin.HandlerFunc Default resource name is {method}:{path}, such as "GET:/api/users/:id" Default block fallback is returning 429 code Define your own behavior by setting options
Types ¶
type Option ¶
type Option func(*options)
func WithBlockFallback ¶
WithBlockFallback sets the fallback handler when requests are blocked.
func WithResourceExtractor ¶
WithResourceExtractor sets the resource extractor of the web requests.
Click to show internal directories.
Click to hide internal directories.