Prepare VPS handover: bootstrap, doctor, DB backup, systemd unit, backend deploy, operations guide; commit docs sources previously ignored

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
denis
2026-10-16 23:15:14 +02:00
parent fb3c539eee
commit a4df92a7c6
43 changed files with 2369 additions and 8 deletions
+45
View File
@@ -0,0 +1,45 @@
# Architecture
## Overview
```text
┌───────────────────────────── Browser ──────────────────────────────┐
│ Interface (ES modules) ──► Worker client ◄──► Web Worker │
│ │ Pyodide / CPython │
│ ▼ │ │
│ IndexedDB: progress, settings persistent data/ │
└────────┬───────────────────────────────────────────────────────────┘
│ reads (optional)
▼ ▼
levels/*.yaml + JSON schema Django backend /api/sync/
```
The application is **self-contained**: everything runs in the browser. The backend is only an optional store for
progress.
## Key decisions
- **Python engine: Pyodide** (real CPython in WebAssembly): faithful error messages, complete standard library. It
runs in a **Web Worker** so the interface never freezes.
- **Interruption** through a `SharedArrayBuffer` (Python's SIGINT): needs an isolated site (COOP/COEP), hence a
Pyodide served from the same domain. `input()` also uses a `SharedArrayBuffer`.
- **Declarative levels**: YAML validated by JSON Schema; validating an exercise goes through a **closed** list of
test types, never through code coming from the level.
- **JavaScript**: ES2022 classes and native modules, no framework.
- **Files**: only `data/` is mounted on IndexedDB (IDBFS) and survives sessions.
- **Drawing**: the `canvas` module records lines returned with the run result; the interface displays them and the
`drawing_*` tests examine them.
- **Narration**: pre-generated mp3 files (Piper, build tool only), falling back to the system voice.
## What lives where
| Folder | Content |
|---|---|
| `app/` | the application (HTML, CSS, `js/core`, `js/ui`, `js/author`, `js/workers`, `python/`) |
| `levels/` | levels, manifest, schema, narration |
| `backend/` | Django sync API |
| `scripts/` | dev server, validation, build, voices |
| `tests/` | `js/` (node:test), `python/`, `e2e/` (Chromium) |
| `docs/site/` | this documentation; `docs/design/`: design notes (in French) |
The detailed reasoning behind each choice is in the repository's `docs/design/`.
+59
View File
@@ -0,0 +1,59 @@
# Sync API
Available only if the Django backend is deployed. It is always on the **same domain** as the application:
`/api/sync/`.
## Authentication
Each request carries a **token**: `Authorization: Bearer <token>`. The token is generated by the client (256 bits,
43 URL-safe characters; the server accepts 32 to 128 `A-Za-z0-9_-` characters). The server only keeps its
**SHA-256**. There is no account and no cookie (hence no CSRF).
## Calls
| Call | Response |
|---|---|
| `GET /api/sync/` | `200 {"payload": …, "updated_at": …}`; `404 {"error": …}` if nothing is stored |
| `PUT /api/sync/` with `{"payload": …, "base": …}` | `200 {"updated_at": …}`; `409` if `base` is stale; `400` invalid or too large payload |
| `DELETE /api/sync/` | `204`: permanently erases this token's data (idempotent) |
| any call without a valid token | `401 {"error": …}` |
- `payload`: the object exported by the application (`version`, `completedLevels`, `badges`, `streak`). Maximum
size: 256 KB.
- `base` (optional): the `updated_at` read by the client. If it no longer matches the server version (another
device wrote in between), the server answers **`409`** with the current state (`payload`, `updated_at`): the
client merges and retries. This is **optimistic concurrency**.
## Who merges?
**The client.** The server stores the last JSON received and merges nothing. Merging keeps the union of completed
levels and badges, the maximum best streak, and the current streak of the most recently active device.
## Client cycle
1. `GET` then local merge;
2. `PUT` of the merged state with `base`; on `409`, merge the received state and retry (4 at most);
3. triggered on activation, at startup, when the network returns, when the tab becomes visible again and 2 seconds
after a local change.
Errors are visible states (`offline`, `token refused`, `server unavailable`, `data refused`, `invalid data`) and
never block the application. A `404` that is not JSON (a static server without Django) is treated as "server
unavailable".
## Editor access (author accounts)
The same backend exposes, for nginx and for the login page:
| Call | Purpose |
|---|---|
| `GET /api/auth/check/` | `204` if the session belongs to an active account with the `authors.use_editor` permission (the "Auteurs" group), otherwise `401`; used by nginx `auth_request` (not reachable from outside) |
| `GET /api/auth/login/` | login form (French or English from `Accept-Language`) |
| `POST /api/auth/login/` | sign in (CSRF); `302` to `next` (local paths only), `200` with a message if refused, `429` when too many attempts |
| `GET /api/auth/me/` | `200 {username, csrf}` for a signed-in author (shows the name and carries the token of the editor's sign-out button), otherwise `401` |
| `POST /api/auth/logout/` | sign out (CSRF); redirects to the login page |
| `/api/admin/` | Django administration (accounts, "Auteurs" group); static files under `/api/static/` |
## Security
Any data received from the server or from a file is treated as untrusted and strictly validated before merging
(types, bounds, identifiers). The server enforces the maximum size; nginx rate-limits.
+36
View File
@@ -0,0 +1,36 @@
# Validation tests
The list of test types (`tests[].kind`) is **closed**: adding a type goes through the engine.
| `kind` | Fields | Passes if… |
|---|---|---|
| `no_syntax_error` | — | the code has no syntax error |
| `stdout_equals` | `value` | the output is **exactly** equal (final newline included) |
| `stdout_contains` | `value` | the output contains this text |
| `stdout_matches_regex` | `value` | the whole output matches the regular expression |
| `var_equals` | `name`, `value` | the variable equals `value` (`10.0` is a `float`, `10` an `int`) |
| `var_type_is` | `name`, `value` | the variable has this type (`str`, `int`, `float`, `list`…) |
| `code_contains` | `value` | the learner's code contains this text (e.g. `random.choice`) |
| `drawing_segments` | `value` | the drawing has this number of lines: `"4"` (exactly) or `"4+"` (at least) |
| `drawing_has_line` | `value` | a line joins two points: `"0,0 100,0"` or with a colour `"0,0 100,0 red"` |
| `drawing_colors` | `value` | all these colours are used: `"red,blue"` |
Common fields: `message` (`fr`/`en` text shown when the test fails) and `inputs` (scripted inputs, see
[Writing a level](../contribute/write-level.md)).
## Details
- **Output**: the `input()` prompt is in the output (as in Python), not the answer.
- **`code_contains`** is not evaluated on a replay: useless with `inputs`.
- **Drawing**: a line counts as present if its ends are within 0.5 of the requested ones, **in either direction**.
Colours: `black`, `red`, `green`, `blue`, `orange`, `purple`, `gray`. The drawing comes from the cell's run (a
blank sheet at each run).
- **A runtime error** always fails the step.
## Adding a test type
1. `ValidationEngine`: interpret the type (and `supportedKinds`).
2. `levels/schema/level.schema.json`: add it to the enumeration.
3. `app/js/author/LevelDraft.js`: add it to `TEST_KINDS`.
4. Editor catalogues: `author.kind.<type>` and its help; the application's `reason.*` messages.
5. `LevelLinter` if consistency rules are needed, then unit tests.