api

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIKeyMiddleware

func APIKeyMiddleware(db *gorm.DB) func(http.Handler) http.Handler

func AdminAuthMiddleware

func AdminAuthMiddleware(secretKey string) func(http.Handler) http.Handler

func CORSMiddleware

func CORSMiddleware(allowAll bool) func(http.Handler) http.Handler

func GetAPIKeyFromContext

func GetAPIKeyFromContext(r *http.Request) *models.APIKey

func MaxBodyBytesMiddleware

func MaxBodyBytesMiddleware(limit int64) func(http.Handler) http.Handler

MaxBodyBytesMiddleware caps the size of request bodies to guard against memory-exhaustion. An oversized body surfaces as a read error, which the JSON-decoding handlers already translate into a 4xx response.

func NewRouter

func NewRouter(db *gorm.DB, secretKey string, cfg RouterConfig) http.Handler

func PerKeyRateLimitMiddleware

func PerKeyRateLimitMiddleware() func(http.Handler) http.Handler

PerKeyRateLimitMiddleware enforces each API key's own RateLimitPerMin budget, independent of the global per-IP limiter. It must run after APIKeyMiddleware so the authenticated key is available on the request context; keys with a limit of 0 are never throttled here.

func RateLimitMiddleware

func RateLimitMiddleware(requestsPerMinute int) func(http.Handler) http.Handler

RateLimitMiddleware limits requests per client IP over a one-minute window and answers with a JSON 429 when the limit is exceeded.

func SecurityHeadersMiddleware

func SecurityHeadersMiddleware(enableHSTS bool) func(http.Handler) http.Handler

SecurityHeadersMiddleware sets a baseline of security-related response headers. HSTS is opt-in because GateCHA is commonly run over plain HTTP locally or behind a TLS-terminating reverse proxy.

Types

type AdminHandler

type AdminHandler struct {
	DB           *gorm.DB
	SecretKey    string
	BuildVersion string
}

func (*AdminHandler) ChangePassword

func (h *AdminHandler) ChangePassword(w http.ResponseWriter, r *http.Request)

POST /api/admin/change-password

func (*AdminHandler) CreateKey

func (h *AdminHandler) CreateKey(w http.ResponseWriter, r *http.Request)

POST /api/admin/keys

func (*AdminHandler) DeleteKey

func (h *AdminHandler) DeleteKey(w http.ResponseWriter, r *http.Request)

DELETE /api/admin/keys/{id}

func (*AdminHandler) GetKey

func (h *AdminHandler) GetKey(w http.ResponseWriter, r *http.Request)

GET /api/admin/keys/{id}

func (*AdminHandler) GetSettings

func (h *AdminHandler) GetSettings(w http.ResponseWriter, r *http.Request)

GET /api/admin/settings

func (*AdminHandler) HISCalibration

func (h *AdminHandler) HISCalibration(w http.ResponseWriter, r *http.Request)

GET /api/admin/his/calibration?key_id=&days= Returns the score distribution and signal averages over stored HIS samples, to help calibrate an enforcement threshold before any blocking is enabled.

func (*AdminHandler) KeyStats

func (h *AdminHandler) KeyStats(w http.ResponseWriter, r *http.Request)

GET /api/admin/stats/keys/{id}

func (*AdminHandler) KeysStatsSummary

func (h *AdminHandler) KeysStatsSummary(w http.ResponseWriter, r *http.Request)

GET /api/admin/stats/keys-summary

func (*AdminHandler) ListKeys

func (h *AdminHandler) ListKeys(w http.ResponseWriter, r *http.Request)

GET /api/admin/keys

func (*AdminHandler) Login

func (h *AdminHandler) Login(w http.ResponseWriter, r *http.Request)

POST /api/admin/login

func (*AdminHandler) Me

GET /api/admin/me

func (*AdminHandler) RotateSecret

func (h *AdminHandler) RotateSecret(w http.ResponseWriter, r *http.Request)

POST /api/admin/keys/{id}/rotate-secret

func (*AdminHandler) StatsOverview

func (h *AdminHandler) StatsOverview(w http.ResponseWriter, r *http.Request)

GET /api/admin/stats/overview

func (*AdminHandler) UpdateKey

func (h *AdminHandler) UpdateKey(w http.ResponseWriter, r *http.Request)

PUT /api/admin/keys/{id}

func (*AdminHandler) UpdateSettings

func (h *AdminHandler) UpdateSettings(w http.ResponseWriter, r *http.Request)

PUT /api/admin/settings

func (*AdminHandler) Version

func (h *AdminHandler) Version(w http.ResponseWriter, r *http.Request)

GET /api/admin/version Returns the build version. Kept behind admin auth so the running version is not disclosed to unauthenticated visitors.

type ChallengeHandler

type ChallengeHandler struct {
	DB *gorm.DB
	// Adaptive scales proof-of-work difficulty by source request rate for keys
	// that opt in via AdaptiveDifficulty.
	Adaptive *adaptiveLimiter
}

func (*ChallengeHandler) ServeHTTP

func (h *ChallengeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type PublicHandler

type PublicHandler struct {
	DB *gorm.DB
}

func (*PublicHandler) LoginConfig

func (h *PublicHandler) LoginConfig(w http.ResponseWriter, r *http.Request)

GET /api/public/login-config

type RouterConfig

type RouterConfig struct {
	CORSAllowAll     bool
	TrustProxy       bool // honor X-Forwarded-For/X-Real-IP (only safe behind a trusted proxy)
	EnableHSTS       bool
	MaxBodyBytes     int64
	RateLimitEnabled bool
	RateLimitLogin   int    // requests per minute on /api/admin/login
	RateLimitAPI     int    // requests per minute on /api/v1/*
	Version          string // build version surfaced to authenticated admins
}

RouterConfig holds the request-handling and hardening options for NewRouter.

type VerifyHandler

type VerifyHandler struct {
	DB *gorm.DB
}

func (*VerifyHandler) ServeHTTP

func (h *VerifyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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