httpapi

package
v0.0.0-...-610d9af Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: GPL-3.0 Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpapi is the HTTP transport layer: routing, middleware, JSON encoding and the mapping from domain errors to status codes. It contains NO business rules; handlers decode a request, call a service, and encode the result.

Request flow (outermost middleware first):

requestID -> accessLog -> metrics -> recoverer -> cors -> rateLimit -> ServeMux
                                                                          |
                       (per route) [csrf, strict rate limit] requireAuth -> handler -> service -> store

requireAuth accepts a Bearer JWT or a session cookie; see its documentation.

Order matters:

  • requestID is outermost so every later log line carries the id.
  • accessLog and metrics sit outside recoverer so they see the 500 that a panic becomes.
  • rateLimit runs before routing and auth, so floods are rejected cheaply, before any token parsing or database work.
  • Nothing between metrics and the mux may replace the *http.Request (see routeOf).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(ctx context.Context, d Deps) http.Handler

NewHandler builds the complete HTTP handler. ctx bounds background goroutines started here (the rate limiter's janitor): cancel it on shutdown.

Types

type Deps

type Deps struct {
	Logger   *slog.Logger
	Auth     *service.AuthService
	Projects *service.ProjectService
	Tasks    *service.TaskService
	Tokens   TokenVerifier
	DB       Pinger

	// SessionAuth (required) powers the cookie-session mode and the /v1/session-auth/* endpoints.
	SessionAuth *service.SessionAuthService
	// GitHub enables "Sign in with GitHub" when non-nil; the OAuth routes are 404 otherwise.
	GitHub *auth.GitHub
	// CookieSecure sets the Secure attribute on cookies. true in production; false only for
	// plain-http local development.
	CookieSecure bool

	// Draining flips to true when shutdown begins; /readyz then returns 503 so the load balancer
	// stops sending new traffic while in-flight requests finish. May be nil.
	Draining *atomic.Bool

	RateRPS     float64
	RateBurst   int
	CORSOrigins []string

	// AuthRateRPS/AuthRateBurst are the (much stricter) per-IP limit for credential endpoints:
	// sign-in, sign-up, password reset, verification mail. Zero falls back to RateRPS/RateBurst.
	AuthRateRPS   float64
	AuthRateBurst int

	// CleanupInterval is how often expired sessions and tokens are deleted (default 1 hour).
	CleanupInterval time.Duration
}

Deps is everything the HTTP layer needs, passed explicitly (no globals, no init()).

type Pinger

type Pinger interface {
	PingContext(ctx context.Context) error
}

Pinger is the slice of *sql.DB the readiness probe needs.

type SessionVerifier

type SessionVerifier interface {
	Authenticate(ctx context.Context, token string) (service.SessionState, error)
}

SessionVerifier resolves a session cookie value. *service.SessionAuthService satisfies it.

type TokenVerifier

type TokenVerifier interface {
	Parse(token string) (auth.Claims, error)
}

TokenVerifier validates a bearer token. *auth.Issuer satisfies it.

Jump to

Keyboard shortcuts

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