Documentation
¶
Overview ¶
internal/api/middleware.go
internal/api/ratelimit.go
internal/api/response.go
Index ¶
- Constants
- func Chain(handler http.HandlerFunc, ...) http.HandlerFunc
- func RequireFeature(enabled bool, featureName string, next http.HandlerFunc) http.HandlerFunc
- func RequireGET(next http.HandlerFunc) http.HandlerFunc
- func RequireMethod(method string, next http.HandlerFunc) http.HandlerFunc
- func RequirePOST(next http.HandlerFunc) http.HandlerFunc
- func RequireQueryParam(paramName string) func(http.HandlerFunc) http.HandlerFunc
- func RequireQueryParams(paramNames []string) func(http.HandlerFunc) http.HandlerFunc
- func WithCORS(next http.HandlerFunc) http.HandlerFunc
- func WithLogging(logger Logger) func(http.HandlerFunc) http.HandlerFunc
- func WithRateLimit(rl *RateLimiter) func(http.HandlerFunc) http.HandlerFunc
- func WithTimeout(timeout time.Duration, next http.HandlerFunc) http.HandlerFunc
- func WriteBadRequest(w http.ResponseWriter, message string)
- func WriteError(w http.ResponseWriter, status int, message string)
- func WriteInternalError(w http.ResponseWriter, message string)
- func WriteJSON(w http.ResponseWriter, status int, data interface{})
- func WriteMethodNotAllowed(w http.ResponseWriter, message string)
- func WriteNotFound(w http.ResponseWriter, message string)
- func WriteSuccess(w http.ResponseWriter, data interface{})
- type HandlerFunc
- type Logger
- type RateLimiter
- type Response
Constants ¶
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 ¶
HandlerFunc is a function type for API handlers that returns data and error.
type Logger ¶
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.