middleware

package
v0.0.0-...-8a6c13a Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxBodyLimit               = 30 << 20 // 30MB
	MaxReqProcsPerSeconds      = 30
	MaxReqGuestTokenPerSeconds = 3
)

Variables

View Source
var Cache = cache.New(cache.Config{
	Expiration:   10 * time.Second,
	CacheControl: true,
})
View Source
var Cors = cors.New(cors.Config{
	AllowMethods: strings.Join(auth.AllowedHttpMethod, ","),
	AllowHeaders: strings.Join([]string{fiber.HeaderContentType, fiber.HeaderContentLength, fiber.HeaderAccept, fiber.HeaderUserAgent, fiber.HeaderAcceptEncoding, fiber.HeaderAcceptCharset, fiber.HeaderAuthorization, fiber.HeaderOrigin, fiber.HeaderLocation, fiber.HeaderKeepAlive}, ","),
})
View Source
var RateLimiterGuestToken = limiter.New(limiter.Config{
	Max: MaxReqGuestTokenPerSeconds,
	KeyGenerator: func(ctx *fiber.Ctx) string {
		var (
			realIp  = ctx.Get(auth.HeaderRealIp)
			xRealIp = ctx.Get(auth.HeaderXRealIp)
		)

		if realIp != "" {
			return utils.CopyString(realIp)
		}

		if xRealIp != "" {
			return utils.CopyString(xRealIp)
		}

		return ctx.IP()
	},
	LimitReached: func(ctx *fiber.Ctx) error {
		return ctx.Status(fiber.StatusTooManyRequests).JSON(&models.ApiError{
			Error: &models.Error{
				Kind:        "too_many_request_token",
				Description: fmt.Sprintf("Maximum Request Exceeded, Maximum %d Request per seconds for guest token", MaxReqGuestTokenPerSeconds),
			},
		})
	},
})
View Source
var RateLimiterProcessing = limiter.New(limiter.Config{
	KeyGenerator: func(ctx *fiber.Ctx) string {

		return utils.CopyString(ctx.Get(fiber.HeaderAuthorization))
	},
	Max: MaxReqProcsPerSeconds,
	LimitReached: func(ctx *fiber.Ctx) error {
		return ctx.Status(fiber.StatusTooManyRequests).JSON(&models.ApiError{
			Error: &models.Error{
				Kind:        "too_many_request",
				Description: fmt.Sprintf("Maximum Request Exceeded, Maximum %d Request per seconds for user", MaxReqProcsPerSeconds),
			},
		})
	},
})

Functions

func AutoDeleteScheduler

func AutoDeleteScheduler(ctx *fiber.Ctx) error

func CatchServerError

func CatchServerError(ctx *fiber.Ctx, err error) error

func CheckAuth

func CheckAuth(ctx *fiber.Ctx) error

func CheckHttpMethod

func CheckHttpMethod(ctx *fiber.Ctx) error

func PurgeAnonymousAccount

func PurgeAnonymousAccount(ctx *fiber.Ctx) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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