api

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package api provides HTTP API handlers for apt-proxy management endpoints.

Package api provides HTTP API handlers for apt-proxy management endpoints.

Package api provides HTTP API handlers for apt-proxy management endpoints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateHitRate

func CalculateHitRate(hits, misses int64) float64

CalculateHitRate calculates the cache hit rate

func FormatBytes

func FormatBytes(bytes int64) string

FormatBytes formats bytes into a human-readable string

func RequireAuth

func RequireAuth(apiKey string, next http.HandlerFunc) http.HandlerFunc

RequireAuth is a middleware function that can be used with standard http.ServeMux. It wraps a handler function with authentication.

func WriteAppError

func WriteAppError(w http.ResponseWriter, err *apperrors.AppError)

WriteAppError writes a structured AppError as HTTP JSON (code + message) for API consistency.

func WriteJSON

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

WriteJSON writes a JSON response with proper encoding

func WriteJSONError

func WriteJSONError(w http.ResponseWriter, statusCode int, errMsg string)

WriteJSONError writes a JSON error response (plain message; for backward compatibility).

Types

type AuthConfig

type AuthConfig struct {
	// APIKey is the required API key for accessing protected endpoints.
	// If empty, authentication is disabled.
	APIKey string

	// HeaderName is the HTTP header name to read the API key from.
	// Defaults to "X-API-Key" if not specified.
	HeaderName string

	// AllowQueryParam allows API key to be passed as a query parameter.
	// The parameter name will be "api_key". Default is false for security.
	AllowQueryParam bool

	// Logger is the structured logger for authentication events.
	Logger *logger.Logger
}

AuthConfig holds authentication configuration for the API middleware.

type AuthMiddleware

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

AuthMiddleware provides API key authentication for protected endpoints.

func NewAuthMiddleware

func NewAuthMiddleware(config AuthConfig) *AuthMiddleware

NewAuthMiddleware creates a new AuthMiddleware with the provided configuration. If config.HeaderName is empty, it defaults to "X-API-Key".

func (*AuthMiddleware) IsEnabled

func (m *AuthMiddleware) IsEnabled() bool

IsEnabled returns true if authentication is enabled (API key is configured).

func (*AuthMiddleware) Wrap

func (m *AuthMiddleware) Wrap(next http.Handler) http.Handler

Wrap wraps an http.Handler with API key authentication. If no API key is configured, the handler is returned without modification. Returns 401 Unauthorized if authentication fails.

func (*AuthMiddleware) WrapFunc

func (m *AuthMiddleware) WrapFunc(next http.HandlerFunc) http.HandlerFunc

WrapFunc wraps an http.HandlerFunc with API key authentication.

type AuthResponseWriter

type AuthResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

AuthResponseWriter wraps http.ResponseWriter to track if a response has been written.

func (*AuthResponseWriter) Write

func (w *AuthResponseWriter) Write(data []byte) (int, error)

Write marks the response as written and writes the data.

func (*AuthResponseWriter) WriteHeader

func (w *AuthResponseWriter) WriteHeader(status int)

WriteHeader captures the status code and marks the response as written.

type CacheCleanupResponse

type CacheCleanupResponse struct {
	Success             bool  `json:"success"`
	ItemsRemoved        int   `json:"items_removed"`
	BytesFreed          int64 `json:"bytes_freed"`
	StaleEntriesRemoved int   `json:"stale_entries_removed"`
	DurationMs          int64 `json:"duration_ms"`
}

CacheCleanupResponse holds the result of a cache cleanup operation

type CacheHandler

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

CacheHandler handles cache-related API endpoints

func NewCacheHandler

func NewCacheHandler(cache httpcache.ExtendedCache, log *logger.Logger) *CacheHandler

NewCacheHandler creates a new CacheHandler

func (*CacheHandler) HandleCacheCleanup

func (h *CacheHandler) HandleCacheCleanup(w http.ResponseWriter, r *http.Request)

HandleCacheCleanup triggers a manual cleanup cycle

func (*CacheHandler) HandleCachePurge

func (h *CacheHandler) HandleCachePurge(w http.ResponseWriter, r *http.Request)

HandleCachePurge clears all cached items

func (*CacheHandler) HandleCacheStats

func (h *CacheHandler) HandleCacheStats(w http.ResponseWriter, r *http.Request)

HandleCacheStats returns cache statistics as JSON

type CachePurgeResponse

type CachePurgeResponse struct {
	Success      bool  `json:"success"`
	ItemsRemoved int   `json:"items_removed"`
	BytesFreed   int64 `json:"bytes_freed"`
}

CachePurgeResponse holds the result of a cache purge operation

type CacheStatsResponse

type CacheStatsResponse struct {
	TotalSizeBytes int64   `json:"total_size_bytes"`
	TotalSizeHuman string  `json:"total_size_human"`
	ItemCount      int     `json:"item_count"`
	StaleCount     int     `json:"stale_count"`
	HitCount       int64   `json:"hit_count"`
	MissCount      int64   `json:"miss_count"`
	HitRate        float64 `json:"hit_rate"`
}

CacheStatsResponse holds cache statistics data

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error"`
	Message string `json:"message,omitempty"`
}

ErrorResponse holds an error message for JSON responses

type MirrorsHandler

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

MirrorsHandler handles mirror-related API endpoints

func NewMirrorsHandler

func NewMirrorsHandler(log *logger.Logger, reloadFunc func()) *MirrorsHandler

NewMirrorsHandler creates a new MirrorsHandler. reloadFunc is optional; if set, HandleMirrorsRefresh will call it (e.g. reload distributions.yaml then refresh mirrors).

func (*MirrorsHandler) HandleMirrorsRefresh

func (h *MirrorsHandler) HandleMirrorsRefresh(w http.ResponseWriter, r *http.Request)

HandleMirrorsRefresh triggers distribution config reload and mirror refresh

type MirrorsRefreshResponse

type MirrorsRefreshResponse struct {
	Success    bool   `json:"success"`
	Message    string `json:"message"`
	DurationMs int64  `json:"duration_ms"`
}

MirrorsRefreshResponse holds the result of a mirrors refresh operation

type RateLimitMiddleware

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

RateLimitMiddleware applies per-IP rate limiting to API handlers. When limit is 0, the next handler is called without limiting.

func NewRateLimitMiddleware

func NewRateLimitMiddleware(limitPerMinute int, log *logger.Logger) *RateLimitMiddleware

NewRateLimitMiddleware creates a middleware that allows limitPerMinute requests per IP per minute. Pass 0 to disable rate limiting (next handler is returned unchanged).

func (*RateLimitMiddleware) Wrap

Wrap wraps an http.Handler with per-IP rate limiting. When limit is 0, returns next unchanged. On rate limit exceeded, responds with 429 and ErrRateLimited.

func (*RateLimitMiddleware) WrapFunc

WrapFunc wraps an http.HandlerFunc with rate limiting.

Jump to

Keyboard shortcuts

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