api

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

internal/api/middleware.go

internal/api/ratelimit.go

internal/api/response.go

Index

Constants

View Source
const DefaultRequestTimeout = 60 * time.Second

DefaultRequestTimeout is the default timeout for HTTP requests.

Variables

This section is empty.

Functions

func Chain

func Chain(handler http.HandlerFunc, middlewares ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc

Chain chains multiple middleware functions together.

func RequireFeature

func RequireFeature(enabled bool, featureName string, next http.HandlerFunc) http.HandlerFunc

RequireFeature wraps a handler to check if a feature is enabled.

func RequireGET

func RequireGET(next http.HandlerFunc) http.HandlerFunc

RequireGET wraps a handler to require GET method.

func RequireMethod

func RequireMethod(method string, next http.HandlerFunc) http.HandlerFunc

RequireMethod wraps a handler to require a specific HTTP method.

func RequirePOST

func RequirePOST(next http.HandlerFunc) http.HandlerFunc

RequirePOST wraps a handler to require POST method.

func RequireQueryParam

func RequireQueryParam(paramName string) func(http.HandlerFunc) http.HandlerFunc

RequireQueryParam returns middleware that checks a required query parameter is present.

func RequireQueryParams

func RequireQueryParams(paramNames []string) func(http.HandlerFunc) http.HandlerFunc

RequireQueryParams returns middleware that checks multiple required query parameters are present.

func WithCORS

func WithCORS(next http.HandlerFunc) http.HandlerFunc

WithCORS wraps a handler to add CORS headers and handle OPTIONS preflight.

func WithLogging

func WithLogging(logger Logger) func(http.HandlerFunc) http.HandlerFunc

WithLogging returns middleware that logs HTTP requests using the provided logger.

func WithRateLimit

func WithRateLimit(rl *RateLimiter) func(http.HandlerFunc) http.HandlerFunc

WithRateLimit returns middleware that applies rate limiting. If the rate limiter is nil, requests pass through without limiting.

func WithTimeout

func WithTimeout(timeout time.Duration, next http.HandlerFunc) http.HandlerFunc

WithTimeout wraps a handler to add a timeout to the request context.

func WriteBadRequest

func WriteBadRequest(w http.ResponseWriter, message string)

WriteBadRequest writes a 400 Bad Request error response.

func WriteError

func WriteError(w http.ResponseWriter, status int, message string)

WriteError writes an error JSON response with the given status code.

func WriteInternalError

func WriteInternalError(w http.ResponseWriter, message string)

WriteInternalError writes a 500 Internal Server Error response.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data interface{})

WriteJSON writes a JSON response with the given status code.

func WriteMethodNotAllowed

func WriteMethodNotAllowed(w http.ResponseWriter, message string)

WriteMethodNotAllowed writes a 405 Method Not Allowed error response.

func WriteNotFound

func WriteNotFound(w http.ResponseWriter, message string)

WriteNotFound writes a 404 Not Found error response.

func WriteSuccess

func WriteSuccess(w http.ResponseWriter, data interface{})

WriteSuccess writes a successful JSON response with status 200.

Types

type HandlerFunc

type HandlerFunc func(ctx context.Context, r *http.Request) (interface{}, error)

HandlerFunc is a function type for API handlers that returns data and error.

type Logger

type Logger func(method, path string, status int, duration time.Duration)

Logger is a function type for logging HTTP requests. It receives method, path, status code, and duration.

type RateLimiter

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

RateLimiter implements a token bucket rate limiter with per-IP tracking.

func NewRateLimiter

func NewRateLimiter(rate float64, burst int) *RateLimiter

NewRateLimiter creates a new rate limiter. rate: requests per second allowed burst: maximum burst size (bucket capacity)

func (*RateLimiter) Allow

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

Allow checks if a request from the given IP is allowed. Returns true if allowed, false if rate limited.

func (*RateLimiter) Stats

func (rl *RateLimiter) Stats() map[string]interface{}

Stats returns current rate limiter statistics.

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop stops the background cleanup goroutine.

type Response

type Response struct {
	Success bool        `json:"success"`
	Data    interface{} `json:"data,omitempty"`
	Error   string      `json:"error,omitempty"`
}

Response is the standard JSON response structure for all API endpoints.

Jump to

Keyboard shortcuts

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