Local Development
Everything in the API repo is driven by mise tasks, which also load the
service’s environment. Prefer mise run <task> over bare go commands so the
env is present.
Prerequisites
Section titled “Prerequisites”- mise ≥ 2026.10.8 (
min_versioninmise.toml; task daemons are an experimental feature it enables). It provisions Go 1.27, sqlc, swag,sql-formatter, golangci-lint, gofumpt,psqldef, the Railway CLI, and more. Migrations run throughcmd/migrate(goose as a library), sogooseis not a standalone provisioned CLI. - Railway access —
mise run envpulls config from Railway andmise run runtunnels to Railway’s Postgres and Redis, so you need to be logged in with project access. - Docker (optional) — only for a throwaway local Postgres/Redis
(
mise run db:local/redis:local).
-
Install tools:
Terminal window mise install -
Pull environment variables from Railway:
Terminal window mise run env # writes .env.json (gitignored); mise auto-loads it -
Run the server:
Terminal window mise run run # go run ./cmd/server, logs piped through logfmtServes on
:${PORT}— health atGET /, Swagger UI atGET /docs/*.
Reaching the database
Section titled “Reaching the database”The Railway databases have no public URL — Postgres and Redis are reachable
only on Railway’s private network. From a laptop you go through Railway’s
tunneling instead: mise.toml declares two mise daemons, db-tunnel and
redis-tunnel, which run railway connect "API Postgres" / "API Redis" with
--tunnel-only for the current ENVIRONMENT (default development) onto
localhost:$DB_PORT / localhost:$REDIS_PORT. mise.toml pins DB_HOST and
REDIS_HOST to localhost, so every task and the server connect through them.
mise daemons start db-tunnel # open the Postgres tunnel on its own (e.g. for db:* tasks or a SQL client)mise daemons # list daemons and their statusmise daemons stop # close the tunnelsmise run run declares both daemons, so it starts any that aren’t already
running before the server boots, and leaves them up after it exits. A local
server therefore talks to that environment’s real database, which Railway has
already migrated (see Deployment); there’s no local
migrate-and-seed step. The db:* tasks don’t start the tunnel themselves — run
mise daemons start db-tunnel first. To reach another environment, set
ENVIRONMENT, re-run mise run env, and mise daemons restart the tunnels.
Changing the schema
Section titled “Changing the schema”db/<schema>/<table>/schema.sql is the source of truth: you edit it in place
to the shape you want, and generate the migration from it. Never hand-write a
migration from scratch or edit one that has already shipped.
-
Edit
schema.sql. Change the table’sCREATE TABLE(and its indexes, triggers, functions) directly. For a new table, add adb/<schema>/<table>/directory withschema.sql+query.sqland a matching entry insqlc.yaml.db/layout_test.goused to fail if a migrated table and its directory didn’t pair up; #506 removed it and relies onmise run db:check(CI’s Schema job) instead. -
Generate the migration (it needs a database — the
db-tunneldaemon or a local container, see the caution below):Terminal window mise daemons start db-tunnelmise run db:schema # ./scripts/schema.sh planIt concatenates
db/_global/*.sql(schema.sqlfirst) and every table’sschema.sql(in migration order, so foreign keys resolve; tables without a migration yet go last), diffs that against the database withpsqldef --dry-run, prints the DDL, and writes it as the-- +goose Uphalf ofdb/_migrations/000NN_CHANGEME.sql. psqldef can’t parse composite types, soschema.shstripsCREATE TYPE … AS (…)blocks before diffing: a composite type (today onlypageindb/_global/page.sql, #505) is neither generated nor drift-checked, and its migration is hand-written (00032_page.sql). -
Finish the file. Rename it (
000NN_<schema>_<table>.sqlfor a new table —schema.shorders by that name — or e.g.00021_bond_bond_bond_number.sqlfor a change), write the-- +goose Downsection, and add anything a diff can’t produce: data backfills,DISABLE/ENABLE TRIGGERaround them, and-- +goose StatementBegin/Endaround plpgsql bodies (00022_bond_attachment.sqldoes all three). -
Regenerate and apply:
Terminal window mise run sql # sqlc reads db/_migrations as its schemamise run db:migrate # against a local Postgres, not the shared onemise run db:check # should now pass
Code generation
Section titled “Code generation”Two generators must be re-run when their inputs change:
mise run sql # sql-formatter + sqlc generate — after editing any query.sql or adding a migrationmise run docs # swag fmt + swag init + jq post-process — after changing handlers/modelsmise run docs runs swag to emit docs/swagger.json (OpenAPI 3.1), then
scripts/openapi_query.jq rewrites the QUERY endpoints
and bumps the spec to OpenAPI 3.2.
Conventions
Section titled “Conventions”- Enums live in
internal/model(type X string+// @name X); DB columns staystring, converted at the boundary. See Architecture. - Service files — each resource
Xin a domain’sServiceinterface getscore_X.go,auth_X.go,cache_X.go, andaudit_X.go, all with the sameX. Methods appear in interface order: Create, Get, List, Update, Delete, then variants,Watch*after the method that triggers it, andPresign*last. Types and constructors live inservice.go(#484). FromDBconverters — everymodeltype has anXFromDB(...); services returnmodel.*, never raw sqlc rows.utils.Mapconverts slices.- Transactions — services
Begin, deriveQueries.WithTx(tx),defer tx.Rollback, and commit at the end. Audit writes happen after commit, in theWithAuditdecorator — core methods never call the audit logger. - Errors — sentinel
model.Errorvalues withWrap/WithMsg;pgx.ErrNoRowsmaps toErrNotFound. Each sentinel carries its own HTTP status (model.Errorimplements Echo’sHTTPStatusCoder), so Echo’sDefaultHTTPErrorHandlerrenders it directly — there is no error-mapping middleware. - IDs — ULIDs everywhere (
internal/id.ULID), stored as Postgresuuid; the zero ULID marshals to SQLNULL/ JSONnull. - Config injection — constructors and initializers that need config take
the whole
*config.Configand reach into the sub-config they need, so callers don’t have to know which slice a package consumes. This is uniform across the service (service.New,db.New,authn.New,storage.New, …). TheAuthConfig/BucketConfig/DBConfigsub-structs (AuthConfigholdsAUTH_JWT_SECRET,AUTH_JWT_ISSUER, and, since #504,AUTH_OTP_SECRET) group related keys (and back the reflection-based check of required env vars, which now runs only whenVALIDATE_CONFIGis set), but they are read inside the package from the passed*config.Configrather than accepted as parameters. - Formatting/lint —
gofumpt+golangci-lint(withrevive). sqlc-generatedinternal/db/**is excluded from lint. - Swagger docstrings — a mandatory godoc first line, tab-indented
annotations in a fixed order (
@Summary → @Description → @Tags → @Accept → @Produce → @Param → @Success → @Failure → @Router), function-scoped request-struct naming, and no comments inside function bodies.
Continuous integration
Section titled “Continuous integration”CI runs on push and mirrors what you can run locally. The unified check:
mise run check # test + fmt + lint + docs| Step | Command | Purpose |
|---|---|---|
| Test | mise run test |
go test ./... |
| Format | mise run fmt |
gofumpt -l -w . |
| Lint | mise run lint |
golangci-lint run |
| Docs | mise run docs |
Regenerate the OpenAPI spec, then fail if the committed docs/ differs — a stale spec can’t merge (#394) |
| Schema | go run ./cmd/migrate up + ./scripts/schema.sh check |
Applies db/_migrations to a throwaway Postgres service, then fails if db/**/schema.sql disagrees — catches a schema.sql edit without its migration and vice versa, except for composite types (see above) |
mise run check doesn’t include the schema check (it needs a database at the
migrations’ head); run mise run db:check yourself, per
Changing the schema. CI pins mise via
jdx/mise-action’s version:, which has to stay at or above min_version —
an older mise rejects the [daemons] tables and fails every job at setup.
A separate release.yaml workflow runs when a GitHub release is published and
fast-forwards production to the release tag (#494); see
Deployment › The promotion model.
Deployment
Section titled “Deployment”Railway builds the API from its Dockerfile, which ships a ./migrate binary
next to ./server (building it also validates the embedded migration set).
Each environment’s pre-deploy command is ./migrate up: it runs once,
between build and deploy, and a failure halts the deployment while the old
version keeps serving. The server never migrates on startup — goose takes no
lock by default, and rolling deploys overlap instances. The pre-deploy command
is a per-environment Railway dashboard setting, not committed config.
Because the old version is still serving while the new migration applies, keep migrations backward compatible with the code already deployed (expand, deploy, then contract).
SDK generation
Section titled “SDK generation”The TypeScript and Python SDKs are generated from this repo’s CI (not from
the SDK repos), driven directly by the Speakeasy CLI. The artifact repos
(api-client-typescript, api-client-python) are pure build outputs — the
authoritative Speakeasy config lives here under .speakeasy/.
graph LR
A[push to staging<br/>docs/** spec changed] --> B[build-spec<br/>mise run docs]
B --> C[swagger.json artifact]
C --> D[generate: typescript]
C --> E[generate: python]
D --> F["@huddlesurety/api<br/>→ GitHub Packages"]
E --> G["huddlesurety-api<br/>→ git-installed"]
-
Trigger — a
pushto thestagingbranch that touches the committed spec or SDK config:docs/**,scripts/**,.swaggo,mise.toml,.speakeasy/**, or the workflow itself (plus manualworkflow_dispatch). Runs are serialized. -
build-spec— runsmise run docsand uploadsdocs/swagger.json. -
generate(matrix: TypeScript + Python) — each language uses its own Speakeasy workspace/key, clones its artifact repo, drops in the spec +.speakeasy/<lang>/config, computes a deterministic version0.1.<commit-count>, runsspeakeasy run --set-version, and pushes to the artifact repo’smainif anything changed.
The version is the api commit count, so generate checks out the full history
(fetch-depth: 0) and refuses to publish from a shallow clone or one with
fewer than 700 commits (#476). A shallow count used to come out as 1, and
because 0.1.1 was already published, the publish step skipped and the run went
green without shipping an SDK.
- TypeScript — package
@huddlesurety/api, classHuddleAPI, published to GitHub Packages (restricted). Consumers authenticate installs with aGITHUB_TOKENthat hasread:packages. - Python — package
huddlesurety-api, classHuddleAPI, git-installed (no PyPI publish).