Documentation ¶
Overview ¶
This package provides Sentinel middleware for echo.
Example ¶
r := echo.New() r.Use(middleware.Logger()) r.Use( SentinelMiddleware( // customize resource extractor if required // method_path by default WithResourceExtractor(func(ctx echo.Context) string { if res, ok := ctx.Get("X-Real-IP").(string); ok { return res } return "" }), // customize block fallback if required // abort with status 429 by default WithBlockFallback(func(ctx echo.Context) error { return ctx.JSON(400, map[string]interface{}{ "err": "too many requests; the quota used up", "code": 10222, }) }), ), ) r.GET("/test", func(c echo.Context) error { return nil }) r.Logger.Fatal(r.Start(":1323"))
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SentinelMiddleware ¶
func SentinelMiddleware(opts ...Option) echo.MiddlewareFunc
SentinelMiddleware returns new echo.HandlerFunc. Default resource name pattern is {httpMethod}:{apiPath}, such as "GET:/api/:id". Default block fallback is to return 429 (Too Many Requests) response.
You may customize your own resource extractor and block handler 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.