Skip to main content

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:

  • /health and /flags need no token.
  • POST /v1/feedback has 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, method and path, 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 with status, duration_ms, bytes and route (the matched pattern, like GET /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:

  1. reads the claims API Gateway already verified: sub (the Cognito user ID), email and name;
  2. adds user_id to the request's logger;
  3. 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);
  4. refuses a closed account with 401;
  5. copies a changed email from the token onto the row, which is how Change email finishes;
  6. 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 in access.go answer 404 when the record is missing, archived, or its team is closed.
  • Check membership. requireTeamRole answers 403 unless the caller belongs to the team the record belongs to. Pass true to 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: WriteJSON for success, WriteError for a 4xx with a message, and ServerError for a 500. ServerError logs 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.

TableKeyHolds
useruser_idA caregiver: name, email, notification setting, open or closed.
deviceuser_id, device_idA caregiver's phones, for push notifications. Nothing uses it yet.
teamteam_idA care team.
membershipuser_id, team_idWho is on which team, and whether they are admin or caregiver.
invitationcodeA pending invitation; expires on its own (DynamoDB TTL).
receiverreceiver_idA care receiver and their team.
care-instructionsreceiver_idA receiver's care instructions and emergency contacts.
trackertracker_idWhat is tracked, its fields and ranges.
scheduleschedule_idWhen a tracker expects entries (a repeat rule).
entryentry_idLogged, skipped and scheduled entries.
journal-notenote_idFree-text notes on a receiver's timeline.
coverage-schedulecoverage_schedule_idRepeating "who is on duty" rules.
assignmentassignment_idOne 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​

  1. Contract. Add the path and its schemas to shared/openapi/openapi.yaml, with a tags entry so it lands in the right group of the reference. On commit, a hook regenerates the Go, TypeScript and Swift clients (Contract and codegen).
  2. Handler. Write the handler and its test in api/internal/v1/, following the load → check membership → work pattern.
  3. Route. Add a line to api/cmd/lambda/mux.go.
  4. API Gateway. Add the path to authedRoutes in infra/lib/api-stack.ts, and update the route count in infra/test/api-stack.test.ts. Without this step the route 404s in AWS.
  5. 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 the api/features scenarios, in-process, against DynamoDB Local in Docker.
  • make -C api smoke-dev runs the same scenarios against the real dev API. It needs AWS credentials.