Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
An iOS automation in Health Auto Export POSTs Apple Health to /api/vitals/ingest, and the new Salud tab (next to Plan) draws whatever arrived: steps, heart rate, sleep, weight, oxygen, workouts and every other metric HAE can export, grouped into cards with a trend, a sparkline and a 7/30/90/365-day range. Nothing on that screen is an input, and the watch's workouts stay out of the history on purpose — they carry no sets and no weights, so folding them into progression, PRs and the recovery model would be inventing data. The interesting part is duplicates. A five-minute automation re-sends overlapping windows: with Date Range "Today", appending would count a day of steps again on every run, and with "Since Last Sync", replacing would throw away everything but the last five minutes. So the endpoint decides per payload instead. Datapoints that sit on midnight are HAE's own day grouping, i.e. a *total*, and a day total replaces the day it names — re-sending the same day all afternoon converges on that day's total. Anything else is a real sample, identified by its instant plus its numbers and stored once, which is right for overlapping and disjoint windows alike. Every sync answers with a receipt (received / duplicates / stored), so a misconfigured automation is visible rather than silent, and day keys come from the date the phone wrote — never the server's clock, or every evening's steps would land on tomorrow. Per profile and private by construction. Each profile mints its own ingest key (Salud → ⚙), which is write-only: it can add to that one profile's health file and cannot read a single number back, so a key leaking out of a phone is a nuisance rather than a disclosure. It is its own credential, so rotating it disconnects a phone without signing anybody out anywhere. The data lives in data/vitals-<uid>.json, outside the synced state document, so a year of heart rate never round-trips through localStorage on every device, and it is bounded on write: 400 days per metric, a 72 h dedupe ledger, the newest 300 workouts. Two traps that only exist in production, both fixed here and both of which would have shipped broken. api/Dockerfile copies the modules server.js imports by name, and vitals.js was not on that list — a container that boots straight into ERR_MODULE_NOT_FOUND. And the web container's nginx capped every /api body at 5 MB, which a health export with Summarize Data off exceeds; that one route now gets the API's own 32 MB while every other path keeps 5 MB, with the shared proxy settings hoisted to server level so the two locations cannot drift apart. Coverage: vitals.js (13 tests — overlapping, disjoint, day-grouped, raw, heart rate, sleep, blood pressure, pruning, timezones), the routes against a real server (4 — isolation between profiles, a key that cannot read, ten re-sends changing nothing), the card model (18) and the screen (4). docs/HEALTH.md has the Health Auto Export settings and the merge table; the five endpoints are in api/openapi.yaml under the health tag, and website/api.html is rebuilt from it.
130 lines
7.6 KiB
Plaintext
130 lines
7.6 KiB
Plaintext
# KuentaGYM — standalone web server: serves the static app AND proxies /api to the backend,
|
|
# so the whole app lives on ONE origin (required for WebAuthn / passkeys).
|
|
server {
|
|
listen ${NGINX_PORT};
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
# Sent on every response below. NOTE: an `add_header` inside a location replaces the whole
|
|
# inherited set rather than adding to it, so the two locations further down that set their
|
|
# own Cache-Control have to repeat these three. Deliberately not a full Content-Security-
|
|
# Policy: only frame-ancestors, which no part of the app depends on. TLS, HSTS and any
|
|
# stricter policy stay the reverse proxy's job (see SECURITY.md).
|
|
add_header X-Frame-Options "DENY" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header Referrer-Policy "same-origin" always;
|
|
add_header Content-Security-Policy "frame-ancestors 'none'" always;
|
|
|
|
# API → backend container.
|
|
# Resolved per request through the container runtime's DNS, not once at start-up: nginx
|
|
# otherwise pins the api container's IP when it boots, and a recreated api (new IP) means
|
|
# 502 on every /api call until web is restarted too (issue #16). The variable is what makes
|
|
# nginx re-resolve; lowercase so the entrypoint's envsubst leaves it alone.
|
|
#
|
|
# RESOLVER defaults to Docker's embedded DNS, which is where this image usually runs.
|
|
# Nothing answers on that address outside Docker, and an unreachable resolver does not
|
|
# fail fast — every /api request hangs until it times out. Anywhere else, set RESOLVER to
|
|
# the DNS the platform actually provides: on Kubernetes that is the kube-dns/CoreDNS
|
|
# service address (`kubectl -n kube-system get svc kube-dns`, commonly 10.96.0.10).
|
|
#
|
|
# These live at server level, not inside the /api/ location, because there is more than one
|
|
# proxied location now (the health ingest takes bigger bodies than everything else) and the
|
|
# two must not be allowed to drift: proxy_set_header, proxy_http_version and resolver all
|
|
# inherit, so the only per-location difference is proxy_pass and the body limit. The
|
|
# CF-Connecting-IP comment below is the security-relevant half of this block — read it
|
|
# before moving anything back into a location.
|
|
resolver ${RESOLVER} valid=10s ipv6=off;
|
|
set $api_upstream http://${BACKEND}:${PORT};
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
# $remote_addr, NOT $proxy_add_x_forwarded_for: the latter APPENDS to whatever the client
|
|
# sent, and the API reads the first entry — so a client could put any address it liked in
|
|
# front of its own and choose what the activity log recorded about it. Overwriting is what
|
|
# makes the header mean anything.
|
|
proxy_set_header X-Forwarded-For $remote_addr;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
# Same reasoning, and the API trusts this one FIRST. Empty by default, which drops the
|
|
# header entirely, so a direct client cannot forge it. Cloudflare users set
|
|
# CF_CONNECTING_IP=$http_cf_connecting_ip to let the real one through — correct only when
|
|
# Cloudflare is genuinely in front, because that is what overwrites a client-supplied one.
|
|
proxy_set_header CF-Connecting-IP "${CF_CONNECTING_IP}";
|
|
|
|
# NOTE on the two /api locations below: both are `^~` prefixes, and prefix locations are
|
|
# matched longest-first, so the ingest one wins for its own path and nothing else changes.
|
|
# `^~` on both so they beat the regex locations further down: without it a request for a path
|
|
# such as /api/anything.json would be matched by the \.(js|css|json|html)$ block instead and be
|
|
# looked for on disk rather than proxied.
|
|
|
|
# Apple Health ingest (POST /api/vitals/ingest). Health Auto Export sends whole exports — a
|
|
# first sync of a year of raw samples is tens of megabytes — so this one route is allowed the
|
|
# API's own VITALS_MAX_BODY (32 MB in api/server.js). It is the ONLY route with that limit:
|
|
# nginx buffers a request body before forwarding it, so a bigger cap anywhere else would let
|
|
# an unauthenticated caller make web hold 32 MB per request.
|
|
location ^~ ${BASE_PATH}/api/vitals/ingest {
|
|
client_max_body_size 32m;
|
|
proxy_pass $api_upstream;
|
|
}
|
|
|
|
location ^~ ${BASE_PATH}/api/ {
|
|
# Match the API's own body limit (MAX_BODY in api/server.js): the whole workout history
|
|
# travels in one PUT /api/data, and nginx's 1 MiB default would answer 413 here before the
|
|
# API ever saw it — a sync that silently stops once the history grows past 1 MiB.
|
|
client_max_body_size 5m;
|
|
proxy_pass $api_upstream;
|
|
}
|
|
|
|
# The app routes behind the hash (/#/plan, /#/history …), so a path is never a screen, and the
|
|
# shell's asset URLs are relative (vite base './' — the Pages demo lives under /demo/). Serving
|
|
# index.html AT a deep path (/plan/r/x: a typo, a stale link) gave a page whose scripts resolved
|
|
# under that path and 404'd — blank, with a 200. Send the browser to the root instead. The
|
|
# query string travels along; the hash never reaches the server anyway. Relative Location so
|
|
# the redirect keeps the scheme and host the reverse proxy in front presented, not nginx's own.
|
|
#
|
|
# BASE_PATH (default empty) is for a reverse proxy that serves KuentaGYM under a subpath and
|
|
# passes that prefix through — "/gym", no trailing slash. A proxy that strips the prefix
|
|
# before the container sees it needs nothing set here: the app asks its own address for the
|
|
# API (lib/api.js appBase), so the call carries whatever prefix the browser is using.
|
|
location ${BASE_PATH}/ {
|
|
# `rewrite ... break` rather than `alias`: try_files inside an aliased prefix location is
|
|
# a long-standing nginx trap (it still resolves the untranslated URI). With BASE_PATH
|
|
# empty this rewrite is an identity and the block behaves exactly as it always has.
|
|
rewrite ^${BASE_PATH}/(.*)$ /$1 break;
|
|
try_files $uri $uri/ @root;
|
|
}
|
|
location @root {
|
|
absolute_redirect off;
|
|
return 302 ${BASE_PATH}/$is_args$args;
|
|
}
|
|
|
|
# App shell: always revalidate so updates show up immediately
|
|
location ~* \.(js|css|json|html)$ {
|
|
# A regex location wins over the prefix block above, so it never saw its rewrite and
|
|
# looked for the file under the prefix on disk. Same identity-when-empty rewrite here.
|
|
rewrite ^${BASE_PATH}/(.*)$ /$1 break;
|
|
add_header Cache-Control "no-cache, must-revalidate";
|
|
add_header X-Frame-Options "DENY" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header Referrer-Policy "same-origin" always;
|
|
add_header Content-Security-Policy "frame-ancestors 'none'" always;
|
|
}
|
|
|
|
# Exercise images/GIFs never change → cache hard
|
|
location ~* \.(png|jpg|jpeg|gif|ico|svg|woff|woff2|webp|avif)$ {
|
|
# A regex location wins over the prefix block above, so it never saw its rewrite and
|
|
# looked for the file under the prefix on disk. Same identity-when-empty rewrite here.
|
|
rewrite ^${BASE_PATH}/(.*)$ /$1 break;
|
|
expires 30d;
|
|
add_header Cache-Control "public, max-age=2592000, immutable";
|
|
add_header X-Frame-Options "DENY" always;
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header Referrer-Policy "same-origin" always;
|
|
add_header Content-Security-Policy "frame-ancestors 'none'" always;
|
|
}
|
|
|
|
gzip on;
|
|
gzip_types text/plain text/css application/json application/javascript image/svg+xml;
|
|
gzip_vary on;
|
|
gzip_min_length 1024;
|
|
}
|