middleware

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: Unlicense Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeServiceNotFound = "SERVICE_NOT_FOUND"
	CodeInvalidRequest  = "INVALID_REQUEST"
	CodeOpenRouterError = "OPENROUTER_ERROR"
	CodeRateLimited     = "RATE_LIMITED"
	CodeInternalError   = "INTERNAL_ERROR"
	CodeMissingHeader   = "MISSING_HEADER"
	CodeUnauthorized    = "UNAUTHORIZED"
)

Error code constants

Variables

This section is empty.

Functions

func AdminAuth

func AdminAuth(apiKey string) fiber.Handler

AdminAuth returns middleware that validates a Bearer token against the configured API key. If apiKey is empty, auth is disabled (passthrough).

func CORS

func CORS() fiber.Handler

CORS returns middleware that sets permissive CORS headers.

func ErrorHandler

func ErrorHandler(c fiber.Ctx, err error) error

ErrorHandler is the custom Fiber error handler.

func GetDBFromContext

func GetDBFromContext(c fiber.Ctx) (*sql.DB, error)

GetDBFromContext retrieves the DB connection stored by ServiceContext middleware.

func GetServiceFromContext

func GetServiceFromContext(c fiber.Ctx) (*service.Service, error)

GetServiceFromContext retrieves the service stored by ServiceContext middleware.

func RateLimit

func RateLimit(limiter *RateLimiter) fiber.Handler

RateLimit returns middleware that applies the given RateLimiter.

func RequestLogger

func RequestLogger() fiber.Handler

RequestLogger returns middleware that logs every request with structured fields.

func ServiceContext

func ServiceContext(mgr service.ServiceManager) fiber.Handler

ServiceContext returns middleware that extracts X-Service-ID and loads the service and its DB connection into the request context.

func SetDBInContext

func SetDBInContext(c fiber.Ctx, db *sql.DB)

SetDBInContext stores a DB connection in the request context. Use this instead of c.Locals directly to avoid Fiber closing the connection.

Types

type AppError

type AppError struct {
	Status  int
	Code    string
	Message string
}

AppError is a structured application error.

func NewInternalError

func NewInternalError(msg string) *AppError

func NewInvalidRequestError

func NewInvalidRequestError(msg string) *AppError

func NewMissingHeaderError

func NewMissingHeaderError(header string) *AppError

func NewOpenRouterError

func NewOpenRouterError(msg string, status int) *AppError

func NewServiceNotFoundError

func NewServiceNotFoundError(id string) *AppError

func NewUnauthorizedError

func NewUnauthorizedError(msg string) *AppError

func (*AppError) Error

func (e *AppError) Error() string

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter implements an in-memory per-IP token bucket rate limiter.

func NewRateLimiter

func NewRateLimiter(requestsPerSecond int) *RateLimiter

NewRateLimiter creates a rate limiter allowing requestsPerSecond per IP.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(ip string) bool

Allow checks whether the given IP is allowed to make a request.

func (*RateLimiter) Close

func (rl *RateLimiter) Close()

Close stops the background cleanup goroutine.

Jump to

Keyboard shortcuts

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