Documentation
¶
Index ¶
- Constants
- func Auth(mode, apiKey string) func(http.Handler) http.Handler
- func CORS(allowedOrigins []string) func(http.Handler) http.Handler
- func Capture(out *CapturedResponse) func(http.Handler) http.Handler
- func ClearSessionCookie(w http.ResponseWriter, mode string)
- func GatewayResolver(next http.Handler) http.Handler
- func GetGateway(r *http.Request) string
- func IssueToken(secret, userID, workspaceID string, ttl time.Duration) (string, error)
- func Logger(env, service string) func(http.Handler) http.Handler
- func ReadBody(r *http.Request) []byte
- func RequestID(next http.Handler) http.Handler
- func RequireAuth(next http.Handler) http.Handler
- func RequireSession(w http.ResponseWriter, r *http.Request) bool
- func Session(secret, mode string) func(http.Handler) http.Handler
- func SetSessionCookie(w http.ResponseWriter, token, mode string)
- func TraceID(ctx context.Context) string
- func UserIDFromContext(ctx context.Context) (string, bool)
- func WorkspaceIDFromContext(ctx context.Context) (string, bool)
- type CapturedRequest
- type CapturedResponse
- type Claims
- type RateLimiter
Constants ¶
const ( SessionCookieName = "testpay_session" // LocalWorkspaceID mirrors store.LocalWorkspaceID; duplicated to avoid circular import. LocalWorkspaceID = "00000000-0000-0000-0000-000000000001" )
const GatewayKey contextKey = "gateway"
const RequestIDHeader = "X-Request-ID"
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
CORS returns a middleware that sets permissive CORS headers for the allowed origins. Credentials are allowed (required for the httpOnly session cookie). Empty allowedOrigins means allow the request's Origin header (for local mode).
func Capture ¶
func Capture(out *CapturedResponse) func(http.Handler) http.Handler
Capture wraps the handler so the response is recorded into *CapturedResponse.
func ClearSessionCookie ¶
func ClearSessionCookie(w http.ResponseWriter, mode string)
ClearSessionCookie writes an expired cookie.
func GatewayResolver ¶
GatewayResolver infers the gateway name from the URL prefix and stores it in context.
func GetGateway ¶
func IssueToken ¶
IssueToken returns a signed JWT for the given user + workspace.
func Logger ¶
Logger injects a contextualized zerolog logger (with trace_id) into the request context, then emits a single "request completed" log at the edge. Downstream code retrieves the logger via log.Ctx(r.Context()).
func RequestID ¶
RequestID extracts or generates a trace ID and stores it in both the response header and the request context.
func RequireAuth ¶
RequireAuth is middleware that 401s if no authenticated session is present. Apply to dashboard /api routes (not /api/auth/*). Mock endpoints do their own api_key-based auth and should not use this.
func RequireSession ¶
func RequireSession(w http.ResponseWriter, r *http.Request) bool
RequireSession writes a 401 if no user is in context and returns false. Call at the top of any handler that requires an authenticated user.
func Session ¶
Session populates context with user_id and workspace_id if the session cookie is valid. No anonymous fallback — dashboard access is always login-gated. Mock endpoints resolve their workspace via api_key Bearer auth.
func SetSessionCookie ¶
func SetSessionCookie(w http.ResponseWriter, token, mode string)
SetSessionCookie writes the session cookie.
func UserIDFromContext ¶
UserIDFromContext returns the authenticated user id if present.
Types ¶
type CapturedRequest ¶
CapturedRequest holds a snapshot of the incoming request.
type CapturedResponse ¶
CapturedResponse holds a snapshot of the outgoing response.
type Claims ¶
type Claims struct {
WorkspaceID string `json:"workspace_id"`
jwt.RegisteredClaims
}
func ParseToken ¶
ParseToken validates and returns the claims or error.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter enforces per-IP and global token-bucket caps. In-memory only; state is lost on restart, which is fine for free-tier abuse prevention (the server also sleeps, so long-term accumulation doesn't matter).
func NewRateLimiter ¶
func NewRateLimiter(perMinute, burstSize, globalPerMin int) *RateLimiter
NewRateLimiter returns a limiter that allows roughly perMinute requests per IP with a burst of burstSize, and caps all clients combined at globalPerMin. Either value <=0 disables that tier.
func (*RateLimiter) Middleware ¶
func (rl *RateLimiter) Middleware(next http.Handler) http.Handler
Middleware returns the HTTP middleware. Requests over the limit get 429.