middleware

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	IdempotencyHeader = "Idempotency-Key"
	// Tells the caller the response came from the store, not from a new send.
	IdempotencyReplayHeader = "X-Idempotent-Replay"

	IdempotencyTTL = 24 * time.Hour
)
View Source
const HeaderRequestID = "X-Request-ID"

Variables

This section is empty.

Functions

func AddUserInfo

func AddUserInfo(userService *userSvc.Service) gin.HandlerFunc

func Auth

func Auth(secret string) gin.HandlerFunc

func AuthWithOptions

func AuthWithOptions(opts AuthOption) gin.HandlerFunc

func ClearDashboardCookie

func ClearDashboardCookie(c *gin.Context)

func DashboardAuth

func DashboardAuth(secret string, users UserProvider) gin.HandlerFunc

func GetClientIP

func GetClientIP(c *gin.Context) string

func IPRateLimit

func IPRateLimit(opts IPRateLimitOption) gin.HandlerFunc

IPRateLimit enforces per-IP limits for public routes.

func Idempotency added in v1.2.0

func Idempotency(repo storage.IdempotencyRepository, log *zap.Logger) gin.HandlerFunc

Idempotency makes a retried POST safe: the first request stores its result under the caller's key and a repeat replays it, instead of sending the same message to WhatsApp twice. Follows draft-ietf-httpapi-idempotency-key-header: 409 while the original still runs, 422 when the key comes back with a different payload.

Without the header the request goes straight through, so callers that do not send a key behave exactly as before.

func IsPrivateIP

func IsPrivateIP(ip string) bool

func RateLimit

func RateLimit(opts RateLimitOption) gin.HandlerFunc

RateLimit counts requests per token using Redis.

func RequestID

func RequestID() gin.HandlerFunc

func RequireAdmin

func RequireAdmin(userService *userSvc.Service) gin.HandlerFunc

func RequireRole

func RequireRole(userService *userSvc.Service, requiredRole string) gin.HandlerFunc

func SentryReport added in v1.2.0

func SentryReport() gin.HandlerFunc

SentryReport reports to Sentry any errors accumulated via c.Error() and any 5xx responses, tagged with the route and request-id. It only runs when Sentry is enabled (conditional registration in the router).

func SetDashboardCookie

func SetDashboardCookie(c *gin.Context, token string)

Types

type AuthOption

type AuthOption struct {
	JWTSecret       string
	APITokenService *apiTokenSvc.Service
	InstanceRepo    storage.InstanceRepository
}

AuthOption configures the authentication middleware.

type IPRateLimitOption

type IPRateLimitOption struct {
	Enabled        bool
	Requests       int
	WindowSeconds  int
	Limiter        ratelimiter.Limiter
	Logger         *zap.Logger
	SkipPrivateIPs bool
}

type RateLimitOption

type RateLimitOption struct {
	Enabled  bool
	Requests int
	Window   time.Duration
	Prefix   string
	Limiter  ratelimiter.Limiter
	Logger   *zap.Logger
}

RateLimitOption configures the per-token rate limit middleware.

type UserProvider

type UserProvider interface {
	Exists(ctx context.Context, id string) (bool, error)
}

Jump to

Keyboard shortcuts

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