fiber

package module
v0.0.0-...-38e494b Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

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

func WithBlockFallback(fn func(ctx *fiber.Ctx) error) Option

WithBlockFallback sets the fallback handler when requests are blocked.

func WithResourceExtractor

func WithResourceExtractor(fn func(*fiber.Ctx) 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