How a request works
The API is one Go program, deployed as one AWS Lambda function. API Gateway sits in front and sends
every route to it. Inside, Go's standard net/http router picks the handler. There is no web
framework.
This page follows one request, GET /v1/trackers/{trackerId} ("show me this tracker"), from the
phone to DynamoDB and back. Every other endpoint takes the same path.
1. API Gateway checks the token
infra/lib/api-stack.ts
defines an HTTP API with a Cognito JWT authorizer. Every /v1 route is listed in the
authedRoutes array there, and API Gateway refuses a request with a missing or invalid token before
the Lambda runs. A path that is not listed returns 404 from API Gateway, even if the Go code
handles it. That is the most common surprise when adding an endpoint.
Two exceptions:
/healthand/flagsneed no token.POST /v1/feedbackhas no authorizer, because feedback can be sent signed out. The Lambda checks the token itself when there is one, and the route has its own throttle (1 request a second, bursts of 5).
2. The Lambda starts and hands the request to Go's router
api/cmd/lambda/main.go
reads configuration from environment variables, builds the router once per cold start, then turns
each API Gateway event into an ordinary http.Request (using aws-lambda-go-api-proxy).
api/cmd/lambda/mux.go
is the routing table. Each line pairs a method and path with a handler:
mux.Handle("GET /v1/trackers/{trackerId}", v1authn.Wrap(http.HandlerFunc(v1trk.Get)))
Read it top to bottom to see every endpoint the API serves. It also builds the stores, one per
DynamoDB table, using table names from V1_*_TABLE environment variables that the CDK sets.
3. Logging wraps everything
api/internal/middleware/logging.go
wraps the whole router. For every request it:
- creates a logger tagged with
request_id,methodandpath, and puts it on the request's context so every later log line carries them; - recovers from a panic, logs it with the stack, and answers
500; - writes exactly one
"request"line at the end withstatus,duration_ms,bytesandroute(the matched pattern, likeGET /v1/trackers/{trackerId}).
Logs are JSON, so CloudWatch Logs Insights can filter on any of these fields. See Logs for useful queries.
4. Authentication: who is calling
Authenticator.Wrap in
api/internal/v1/auth.go
runs before every /v1 handler. It:
- reads the claims API Gateway already verified:
sub(the Cognito user ID),emailandname; - adds
user_idto the request's logger; - looks up the caregiver's row in the user table, creating it on their very first request ("just-in-time" sign-up, so there is no separate sign-up endpoint);
- refuses a closed account with
401; - copies a changed email from the token onto the row, which is how Change email finishes;
- attaches the caller to the request context for the handler.
5. The handler: what may this caller do
Handlers live in
api/internal/v1/, one
file per area (trackers.go, home.go, teams.go, coverage.go and so on). Here is the whole
handler for our request:
func (h *Trackers) Get(w http.ResponseWriter, r *http.Request) {
c := requireCaller(w, r) // who is calling (step 4)
if c == nil {
return
}
t, _, ok := loadTracker(w, r, h.stores, r.PathValue("trackerId")) // 404 if missing
if !ok || !requireTeamRole(w, r, h.stores, c, t.TeamID, false) { // 403 if not on the team
return
}
h.writeDetail(w, r, http.StatusOK, t, h.now().UTC()) // 200 + JSON
}
Every handler follows this pattern:
- Load the record, plus its receiver and team. The
load…helpers inaccess.goanswer404when the record is missing, archived, or its team is closed. - Check membership.
requireTeamRoleanswers403unless the caller belongs to the team the record belongs to. Passtrueto require the team's admin. The team always comes from the record, never from the request, so a caller cannot reach another team's data by changing an ID. - Do the work and write the response with
httpx:WriteJSONfor success,WriteErrorfor a4xxwith a message, andServerErrorfor a500.ServerErrorlogs the real error but sends the app only a generic message.
6. Stores and DynamoDB
The stores in
shared/go-common/v1/store/
are the only code that talks to DynamoDB. There is one file per entity, each with methods like
Get, Create, or ListByReceiver. The entities themselves (plain Go structs, plus rules such as
"is this reading out of range") are in
shared/go-common/v1/domain/.
Both live in shared/ because the generator uses them too.
Each entity has its own table, named caregiver-{stage}-v1-{entity}, defined in v1TableSpecs in
infra/lib/shared-stack.ts.
The model behind them is ADR 0012.
| Table | Key | Holds |
|---|---|---|
user | user_id | A caregiver: name, email, notification setting, open or closed. |
device | user_id, device_id | A caregiver's phones, for push notifications. Nothing uses it yet. |
team | team_id | A care team. |
membership | user_id, team_id | Who is on which team, and whether they are admin or caregiver. |
invitation | code | A pending invitation; expires on its own (DynamoDB TTL). |
receiver | receiver_id | A care receiver and their team. |
care-instructions | receiver_id | A receiver's care instructions and emergency contacts. |
tracker | tracker_id | What is tracked, its fields and ranges. |
schedule | schedule_id | When a tracker expects entries (a repeat rule). |
entry | entry_id | Logged, skipped and scheduled entries. |
journal-note | note_id | Free-text notes on a receiver's timeline. |
coverage-schedule | coverage_schedule_id | Repeating "who is on duty" rules. |
assignment | assignment_id | One stretch of time a caregiver is on duty. |
Secondary indexes (for example the entry table's receiver-time, used for the timeline) are listed
beside each table in v1TableSpecs.
Adding an endpoint
- Contract. Add the path and its schemas to
shared/openapi/openapi.yaml, with atagsentry so it lands in the right group of the reference. On commit, a hook regenerates the Go, TypeScript and Swift clients (Contract and codegen). - Handler. Write the handler and its test in
api/internal/v1/, following the load → check membership → work pattern. - Route. Add a line to
api/cmd/lambda/mux.go. - API Gateway. Add the path to
authedRoutesininfra/lib/api-stack.ts, and update the route count ininfra/test/api-stack.test.ts. Without this step the route404s in AWS. - Scenario. If a caregiver can see the behavior, add a Gherkin scenario under
api/features/.
Tests
cd api && go test ./...runs the handler tests and theapi/featuresscenarios, in-process, against DynamoDB Local in Docker.make -C api smoke-devruns the same scenarios against the real dev API. It needs AWS credentials.