Compare commits
2
Commits
cc257d2671
...
e1d28f971d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1d28f971d | ||
|
|
f36a32e8af |
No files matched your search
@@ -1,73 +1,3 @@
|
||||
# Build-from-source stack for a host that builds at deploy time (Dokploy and friends).
|
||||
#
|
||||
# This is the same five-service shape the instance at gym.dploy.lol runs: `source` clones the
|
||||
# repository into a volume, `api` installs and runs the backend from that clone, `web-build`
|
||||
# compiles the frontend, and `web` serves the result with nginx. Nothing is pulled from a
|
||||
# registry, so `scripts/build-images.sh` and `.gitea/workflows/docker-publish.yml` are not part
|
||||
# of this path at all.
|
||||
#
|
||||
# Five things here are easy to get wrong, and all five are load-bearing:
|
||||
#
|
||||
# 1. `web-build` copies the WHOLE repository into /app and builds from /app/frontend, not
|
||||
# frontend/ on its own. The frontend imports the Coach's core from outside its own directory
|
||||
# (`frontend/src/lib/coach-local.js` and `views/CoachSetup.jsx` reach into
|
||||
# `../../../api/coach/core/`), so a build stage that only has frontend/ fails with nine
|
||||
# UNRESOLVED_IMPORT errors. The repo's own web/Dockerfile copies api/coach/core/ into its
|
||||
# build stage for the same reason.
|
||||
#
|
||||
# 2. The volumes are pinned with `name:`. They hold the users, passkeys, workouts and weigh-ins
|
||||
# — `kuentagym-data` above all. Those names are this deployment's existing volumes; they are
|
||||
# spelled out so a restyled stack keeps pointing at the same data instead of silently
|
||||
# creating empty ones. Never change them without moving the data first, and never run
|
||||
# `docker compose down -v` — `-v` deletes the volumes, which is every logged set and every
|
||||
# weigh-in. A plain `docker compose down` is safe. Check yours with `docker volume ls`.
|
||||
#
|
||||
# 3. nginx needs `client_max_body_size`, and needs it in two sizes. Its default request limit is
|
||||
# 1 MB, and the app PUTs the whole profile to /api/data on every change — a couple of years of
|
||||
# training passes 1 MB and the sync then fails with a 413 the UI cannot explain. So the bulk
|
||||
# of /api is 5m, matching the API's own MAX_BODY. But the Apple Health ingest takes whole
|
||||
# exports: a first sync of a year of raw samples is tens of megabytes, and the API allows
|
||||
# that one route 32 MB (VITALS_MAX_BODY in api/server.js). One global number cannot do both,
|
||||
# which is why the generated config below has a second location for that one path — nginx
|
||||
# picks the longest matching prefix, so /api/vitals/ingest gets 32m and everything else keeps
|
||||
# 5m.
|
||||
#
|
||||
# This inline config is the one this deployment actually runs; web/nginx.conf.template is for
|
||||
# the registry-image path (docker-compose.yml) and is NOT read here. A change to the limits
|
||||
# has to be made in both, or the two deployment shapes diverge.
|
||||
#
|
||||
# 4. X-Forwarded-For uses the APPEND form here, unlike web/nginx.conf.template. In this shape
|
||||
# nginx is NOT the edge: Traefik terminates TLS in front of it, so `$remote_addr` is Traefik's
|
||||
# own address and the real client only survives in the header Traefik sends.
|
||||
# `$proxy_add_x_forwarded_for` carries that one through as the first entry, which is exactly
|
||||
# what the API reads (see clientIp in api/server.js). The registry template does the opposite
|
||||
# for the opposite reason — there the web container answers the internet directly, so the
|
||||
# incoming header can be forged and only $remote_addr can be trusted.
|
||||
#
|
||||
# 5. nginx resolves `api` through a VARIABLE, not by name. A plain `proxy_pass http://api:3000`
|
||||
# is resolved once, when nginx starts, and the address is then pinned for the life of the
|
||||
# container: recreate the api — a deploy that changes its configuration, a `--force-recreate`,
|
||||
# a manual restart that gives it a new IP — and every /api call answers 502 until the web
|
||||
# container is restarted too, including /api/config, so the app cannot even load. The
|
||||
# `resolver` + `set $api_upstream` pair below makes nginx ask again on every request, which is
|
||||
# the same fix web/nginx.conf.template carries (and explains at length) for the other shape.
|
||||
#
|
||||
# To deploy a change to the code, force the one-shot services to run again — `source` and
|
||||
# `web-build` have `restart: "no"`, so compose leaves them alone when their configuration has not
|
||||
# changed:
|
||||
#
|
||||
# docker compose up -d --force-recreate
|
||||
#
|
||||
# That is also how the `api` service picks up new code: it copies the clone into /app at container
|
||||
# start, so an `api` that was not recreated keeps running the previous revision while `web-build`
|
||||
# happily serves the new frontend. Recreate `web` in the same command, and not on its own: its
|
||||
# `proxy_pass http://api:3000` is resolved once at start-up, so a recreated api means a new IP and
|
||||
# an nginx still pointing at the old one answers 502 on every /api call.
|
||||
#
|
||||
# Set RP_ID, RP_NAME and ORIGIN in the host's environment (or its .env). RP_ID must be the exact
|
||||
# hostname the app is served from — passkeys are bound to it — and RP_NAME is what the passkey
|
||||
# prompt shows, so set it to KuentaGYM.
|
||||
|
||||
name: kuentagym
|
||||
|
||||
services:
|
||||
|
||||
@@ -157,19 +157,38 @@ function InvitesCard({ invites, reload, inviteOnly }) {
|
||||
// Who signed in, who tried and failed, what an admin changed. A card rather than its own route:
|
||||
// the dashboard is deliberately one page of cards, and the 95 % use of this is a glance at the
|
||||
// last twenty events. Paging follows Library.jsx's house style — "Show more", not page numbers.
|
||||
/* How many events the card shows before somebody asks for more.
|
||||
*
|
||||
* The log is history and history is long: twenty-five rows is a wall of text that pushes the rest of
|
||||
* the dashboard off the screen, and a cleared log is a full one again the next day. The count in the
|
||||
* lead line says how much there is — the newest ten are shown, and one button reveals the rest, ten
|
||||
* at a time, fetching from the server once everything already loaded is on screen. */
|
||||
const AUDIT_PAGE = 10
|
||||
|
||||
function AuditCard({ tick }) {
|
||||
const toast = useUI(s => s.toast)
|
||||
const [meta, setMeta] = useState(null) // last response minus the rows: total, retention, …
|
||||
const [rows, setRows] = useState([])
|
||||
const [cat, setCat] = useState('')
|
||||
const [shown, setShown] = useState(AUDIT_PAGE)
|
||||
|
||||
const load = (c, before) => api('/api/admin/audit?limit=50&cat=' + c + (before ? '&before=' + before : ''))
|
||||
.then(r => { setMeta(r); setRows(x => (before ? x.concat(r.events) : r.events)) })
|
||||
// `|| []` because a response without events is a response with no events, not a crash: an
|
||||
// admin card that throws takes the whole dashboard with it.
|
||||
.then(r => { setMeta(r); setRows(x => (before ? x.concat(r.events || []) : (r.events || []))) })
|
||||
.catch(e => toast(e.message))
|
||||
const pick = c => { setCat(c); setRows([]); setMeta(null); load(c) }
|
||||
const pick = c => { setCat(c); setRows([]); setMeta(null); setShown(AUDIT_PAGE); load(c) }
|
||||
// Reloads on mount and whenever the header's ↻ bumps the tick. Deliberately not on the 15s
|
||||
// poll that drives "training now": this is history, not presence.
|
||||
useEffect(() => { load(cat) }, [tick])
|
||||
// poll that drives "training now": this is history, not presence. A reload starts folded again,
|
||||
// which is the point of folding it.
|
||||
useEffect(() => { setShown(AUDIT_PAGE); load(cat) }, [tick])
|
||||
|
||||
const hidden = rows.length - shown
|
||||
const more = () => {
|
||||
setShown(n => n + AUDIT_PAGE)
|
||||
// Everything already fetched is on screen: the next ten come from the server.
|
||||
if (hidden <= 0 && meta?.nextBefore) load(cat, meta.nextBefore)
|
||||
}
|
||||
|
||||
const clear = () => confirmSheet({
|
||||
title: 'Clear the activity log?',
|
||||
@@ -194,7 +213,7 @@ function AuditCard({ tick }) {
|
||||
{[['', 'All'], ['auth', 'Sign-ins'], ['admin', 'Admin'], ['fail', 'Failed']].map(([v, l]) =>
|
||||
<button key={v} className={'chip' + (cat === v ? ' on' : '')} onClick={() => pick(v)}>{l}</button>)}
|
||||
</div>
|
||||
{rows.map(e => {
|
||||
{rows.slice(0, shown).map(e => {
|
||||
const line = auditLine(e)
|
||||
return <div key={e.id} className="row between" style={{ padding: '8px 2px', borderBottom: 'var(--hair) solid var(--sep)' }}>
|
||||
<div className="grow">
|
||||
@@ -208,8 +227,10 @@ function AuditCard({ tick }) {
|
||||
</div>
|
||||
})}
|
||||
{meta && !rows.length && <div className="adm-empty">Nothing logged yet.</div>}
|
||||
{meta?.nextBefore && <div style={{ marginTop: 10 }}>
|
||||
<Button size="sm" onClick={() => load(cat, meta.nextBefore)}>Show more</Button></div>}
|
||||
{(hidden > 0 || meta?.nextBefore) && <div className="row" style={{ marginTop: 10, gap: 10, alignItems: 'center' }}>
|
||||
<Button size="sm" onClick={more}>Show more</Button>
|
||||
{meta && <span className="small muted">{fmtNum(Math.min(shown, rows.length))} of {fmtNum(meta.total)}</span>}
|
||||
</div>}
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,10 @@ import Admin from './Admin.jsx'
|
||||
|
||||
globalThis.IS_REACT_ACT_ENVIRONMENT = true
|
||||
|
||||
const mocks = vi.hoisted(() => ({ answers: {}, sheets: [] }))
|
||||
const mocks = vi.hoisted(() => ({ answers: {}, sheets: [], calls: [] }))
|
||||
vi.mock('../lib/api.js', () => ({
|
||||
api: path => {
|
||||
mocks.calls.push(path)
|
||||
const key = path.split('?')[0]
|
||||
return key in mocks.answers ? Promise.resolve(mocks.answers[key]) : Promise.reject(new Error('not found'))
|
||||
}
|
||||
@@ -48,6 +49,7 @@ const good = { id: 'w1', name: 'Push day', d: '2026-09-10', start: 1000, end: 61
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = ''
|
||||
mocks.sheets.length = 0
|
||||
mocks.calls.length = 0
|
||||
mocks.answers = {
|
||||
'/api/admin/users': { users: [{ id: 'u1', name: 'Mallory', workouts: 2, lastSync: null, disabled: false }], invite_only: false },
|
||||
'/api/admin/invites': { invites: [] },
|
||||
@@ -63,6 +65,71 @@ beforeEach(() => {
|
||||
})
|
||||
afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) })
|
||||
|
||||
/* The activity log is history, and history grows: one line per sign-in, per failed attempt, per
|
||||
admin action. Reported as a wall of twenty-five rows pushing the rest of the dashboard off the
|
||||
screen. It opens folded to the newest ten with the total in the lead line, and one button reveals
|
||||
the rest — from what is already loaded first, and from the server once that runs out. */
|
||||
describe('the activity log', () => {
|
||||
const withLog = (n, extra = {}) => {
|
||||
mocks.answers['/api/admin/audit'] = {
|
||||
events: Array.from({ length: n }, (_, i) => ({ id: n - i, ts: Date.now() - i * 60000, ev: 'auth.login.ok', uid: 'u1', name: 'Daniel', ok: true })),
|
||||
total: n, nextBefore: null, enabled: true, ip_mode: 'off', retention: { days: 90, max: 5000 }, now: Date.now(), ...extra
|
||||
}
|
||||
}
|
||||
const auditCard = page => [...page.querySelectorAll('.card')].find(c => c.textContent.includes('Activity log'))
|
||||
const auditRows = card => [...card.querySelectorAll('.row.between')].filter(r => r.querySelector('.small.muted'))
|
||||
const showMore = card => [...card.querySelectorAll('button')].find(b => b.textContent.trim() === 'Show more')
|
||||
|
||||
it('opens with the newest ten and reveals the rest on demand', async () => {
|
||||
withLog(25)
|
||||
const page = render(<Admin />)
|
||||
await settle()
|
||||
const card = auditCard(page)
|
||||
expect(auditRows(card).length).toBe(10);
|
||||
expect(card.textContent).toContain('25 events')
|
||||
expect(card.textContent).toContain('10 of 25')
|
||||
|
||||
act(() => showMore(card).click())
|
||||
await settle()
|
||||
expect(auditRows(card).length).toBe(20)
|
||||
expect(card.textContent).toContain('20 of 25')
|
||||
|
||||
act(() => showMore(card).click())
|
||||
await settle()
|
||||
expect(auditRows(card).length).toBe(25)
|
||||
// Everything is on screen and nothing is left on the server: the button goes away rather than
|
||||
// sitting there inviting a click that does nothing.
|
||||
expect(showMore(card)).toBeFalsy()
|
||||
})
|
||||
|
||||
it('asks the server for the next page once what it has is shown', async () => {
|
||||
// A page of fifty with more behind it: revealing past the fifty asks for the next page rather
|
||||
// than sitting on a button that does nothing.
|
||||
withLog(50, { total: 80, nextBefore: 3 })
|
||||
const page = render(<Admin />)
|
||||
await settle()
|
||||
const card = auditCard(page)
|
||||
// Four clicks reveal the fifty that were already loaded — and none of them asks the server.
|
||||
for (let i = 0; i < 4; i++) { act(() => showMore(card).click()); await settle() }
|
||||
expect(auditRows(card).length).toBe(50)
|
||||
expect(mocks.calls.some(c => c.includes('before=3'))).toBe(false, 'a page already here is not re-fetched')
|
||||
expect(showMore(card)).toBeTruthy()
|
||||
// The fifth has nothing left to reveal, so it goes and gets the next page.
|
||||
act(() => showMore(card).click())
|
||||
await settle()
|
||||
expect(mocks.calls.some(c => c.includes('before=3'))).toBe(true, 'and then it fetches');
|
||||
})
|
||||
|
||||
it('says nothing to show on an empty log, and no button', async () => {
|
||||
withLog(0)
|
||||
const page = render(<Admin />)
|
||||
await settle()
|
||||
const card = auditCard(page)
|
||||
expect(card.textContent).toContain('Nothing logged yet.')
|
||||
expect(showMore(card)).toBeFalsy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Admin user drill-down', () => {
|
||||
it('opens a profile with malformed workout entries and still offers "Disable account"', async () => {
|
||||
const page = render(<Admin />)
|
||||
|
||||
Reference in new issue
Block a user