api

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 59 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 api provides the HTTP server and route wiring for Hardcount. It creates domain stores, services, and handlers, then mounts them on a chi router with auth, rate limiting, and metrics middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RecordSchedulerRun

func RecordSchedulerRun()

RecordSchedulerRun is called by the billing scheduler after each cycle, so the health check can tell whether the scheduler is alive.

func SetDraining

func SetDraining()

SetDraining announces shutdown: readiness answers 503 from now on.

func SetSchedulerInterval

func SetSchedulerInterval(d time.Duration)

SetSchedulerInterval stores the configured scheduler interval. The health check flags the scheduler as degraded when more than two intervals pass after its last run.

Types

type Server

type Server struct {

	// Exported so main.go can wire the billing scheduler and dunning.
	BillingEngine    *billing.Engine
	DunningSvc       *dunning.Service
	SettingsStore    *tenant.SettingsStore
	WebhookOutSvc    *webhook.Service
	OutboxStore      *webhook.OutboxStore
	EmailOutboxStore *email.OutboxStore
	EmailSender      *email.Sender
	// EmailSettledChecker feeds the email dispatcher's staleness gate.
	// cmd/hardcount passes it to the dispatcher with SetSettledChecker. The
	// dispatcher skips an action-required email whose invoice was settled
	// while it was queued.
	EmailSettledChecker email.InvoiceSettledChecker
	CreditSvc           *credit.Service
	InvoiceSvc          *invoice.Service
	CreditNoteSvc       *creditnote.Service
	SubscriptionSvc     *subscription.Service
	TokenSvc            *payment.TokenService
	PaymentReconciler   *payment.Reconciler
	// LoginThrottle is the sign-in throttle. main.go adds its Cleanup to the
	// leader's cleanup steps.
	LoginThrottle *user.LoginThrottle

	// TestClockSvc lets main.go wire the catch-up queue and worker, and run
	// boot recovery for clocks that an earlier process left 'advancing'.
	// Catch-up runs everything that fell due in the time a test clock
	// skipped, such as trial ends, billing, tax retries, dunning and credit
	// expiries. It runs off the request, as Stripe's test-clock advance does
	// (ADR-015). The service is built here, and main.go runs it with the
	// other background workers.
	TestClockSvc *testclock.Service
	// contains filtered or unexported fields
}

Server is the HTTP API. Its exported fields hold the services and stores that cmd/hardcount wires into the background workers.

func NewServer

func NewServer(db *postgres.DB, clk clock.Clock, rdb *redis.Client, opts ...ServerOption) *Server

NewServer builds every store, service and handler, wires them together and mounts the routes. A nil clk means the real clock. It panics when a required collaborator or audit emitter is not wired, or when the built-in recipes fail to load, so a broken build or wiring stops boot.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the request through the router.

type ServerOption

type ServerOption func(*serverOptions)

ServerOption sets a boot-time value that NewServer passes to the services it builds. cmd/hardcount passes what config.Load read. Tests pass what they exercise, and otherwise run as an install with none of them set.

func WithAppEnv

func WithAppEnv(env string) ServerOption

WithAppEnv sets the environment to config.Config.Env, which is APP_ENV as config.Load normalised it, so every consumer below compares the same string. Without it, NewServer runs as "local", as an unset APP_ENV does.

func WithInvoiceGrace

func WithInvoiceGrace(d time.Duration) ServerOption

WithInvoiceGrace sets INVOICE_GRACE_PERIOD (ADR-120). One value feeds the two places that must agree on when a period is due: the billing runs' due scans and the billing backlog gauges. The credit-expiry sweep does not use it: it holds a grant until the close that draws on it has run (ADR-071 §2).

func WithRateLimits

func WithRateLimits(rl config.RateLimits) ServerOption

WithRateLimits sets the HARDCOUNT_RATE_LIMIT_* limits config.Load parsed. Without it NewServer uses config.DefaultRateLimits.

Directories

Path Synopsis
Package middleware provides HTTP middleware for the Hardcount API server.
Package middleware provides HTTP middleware for the Hardcount API server.
Package respond writes the API's JSON responses: success bodies, list pages and the error envelope.
Package respond writes the API's JSON responses: success bodies, list pages and the error envelope.
Package timefilter parses the ?from and ?to query parameters, and named variants such as ?date_from and ?date_to, for the operator-facing list and export endpoints.
Package timefilter parses the ?from and ?to query parameters, and named variants such as ?date_from and ?date_to, for the operator-facing list and export endpoints.

Jump to

Keyboard shortcuts

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