echo

package module
v0.0.0-...-0807185 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

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

func WithBlockFallback(fn func(ctx echo.Context) error) Option

WithBlockFallback sets the fallback handler when requests are blocked.

func WithResourceExtractor

func WithResourceExtractor(fn func(ctx echo.Context) string) Option

WithResourceExtractor sets the resource extractor of the web requests.

Jump to

Keyboard shortcuts

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