Serve the picks from the API, so the live picker reads them

The picker read circuits/photos.json and seasons/photos.json relative to
itself. The API serves only /examples/, so the live page showed two HTTP
404 errors and a "Load it from disk" box for each.

The API now serves both picks files, read-only, at /picks/circuits.json
and /picks/seasons.json. Each answer is the file as the poster service
has it loaded, from the same loader it draws from: the notes first, then
the entries, a refused entry left out. The root route lists both routes.

The picker reads them from the API its site picks. Only a page opened
from a working copy (file:, or a loopback host) falls back to the
repository's files. A failure names each address and why.

Builds: docs/posters.md, Picking photos, as written in 780984064f (the lead's lines 1-4 of 2026-11-30 and answers Q1, Q2)

Claude-Session: https://claude.ai/code/session_01JG73KFhbaNj3tyJ8tKmE3W
This commit is contained in:
2026-11-30 10:18:16 +01:00
parent 780984064f
commit 250828710c
7 changed files with 148 additions and 51 deletions
+45 -22
View File
@@ -54,9 +54,11 @@
<section class="card" id="files">
<h2>1. The lists</h2>
<p class="hint">The candidates come with the site; the picks are read from the repository, beside the page’s folder.
Run <code>python3 -m http.server</code> in the working copy and open <code>/examples/picker.html</code> there to
have both, or load a picks file from disk below.</p>
<p class="hint">The candidates come with the site; the picks are the API’s, <code>/picks/circuits.json</code> and
<code>/picks/seasons.json</code>, as the poster service draws from them. A page opened from a working copy (from
disk, or served on this machine with <code>python3 -m http.server</code>) reads the working copy’s
<code>circuits/photos.json</code> and <code>seasons/photos.json</code> when the API does not answer; or load a
picks file from disk below.</p>
<div id="cand-file">
<div class="request"></div>
<div class="status"></div>
@@ -140,11 +142,15 @@
const FRAMES = { poster: [1000, 1500], thumb: [1920, 1080] };
const PICK_FIELDS = ['file', 'licence', 'author', 'credit', 'date'];
// The picks are the API's (docs/posters.md, Picking photos): route is its address, local the working copy's file.
const FILES = {
candidates: { card: $('cand-file'), url: 'data/candidates.json', what: 'the candidates (examples/data/candidates.json)' },
circuits: { card: $('picks-circuits'), url: '../circuits/photos.json', what: 'the layouts’ picks (circuits/photos.json)' },
seasons: { card: $('picks-seasons'), url: '../seasons/photos.json', what: 'the seasons’ picks (seasons/photos.json)' },
circuits: { card: $('picks-circuits'), route: '/picks/circuits.json', local: '../circuits/photos.json', what: 'the layouts’ picks (circuits/photos.json)' },
seasons: { card: $('picks-seasons'), route: '/picks/seasons.json', local: '../seasons/photos.json', what: 'the seasons’ picks (seasons/photos.json)' },
};
// A page opened from a working copy: from disk, or served on this machine. Only such a page falls back to the
// working copy's picks files when the API does not answer them; on any other host a failure is said and stays.
const WORKING_COPY = location.protocol === 'file:' || ['localhost', '127.0.0.1', '[::1]'].includes(location.hostname);
const state = {
mode: params.get('mode') === 'season' ? 'season' : 'layout',
@@ -182,30 +188,47 @@
return '';
}
/** Reads one file over http, or offers to load it from disk when that fails; calls done(value) once read. */
/** One address read as JSON and checked: { value }, or { why } it could not be used. */
async function readJSON(url, check) {
try {
const res = await fetch(url, { cache: 'no-cache' });
if (!res.ok) return { why: `HTTP ${res.status}` };
const value = JSON.parse(await res.text());
const why = check(value);
return why ? { why } : { value };
} catch (e) {
return { why: e instanceof SyntaxError ? 'it is not JSON' : `no answer (${e.message})` };
}
}
/**
* Reads one file: the candidates from the site; the picks from the API and, only on a working copy, from its own
* file when the API does not answer them. When every address failed, says each with why and offers to load the
* file from disk; calls done(value) once read.
*/
async function readFile(name, done) {
const f = FILES[name];
const card = f.card;
const out = card.querySelector('.result');
const url = new URL(f.url, location.href).href;
setRequest(card.querySelector('.request'), url, name === 'candidates' ? 'a file of the site, not the API' : 'a file of the repository, not the API');
setStatus(card.querySelector('.status'), 'pending', `Reading ${f.what}…`);
const tries = name === 'candidates'
? [{ url: f.url, label: 'a file of the site, not the API' }]
: [{ url: api(f.route), label: null },
...(WORKING_COPY ? [{ url: f.local, label: 'a file of the working copy, not the API' }] : [])];
const check = name === 'candidates' ? validCandidates : validPicks;
let why = '';
try {
const res = await fetch(url, { cache: 'no-cache' });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const value = JSON.parse(await res.text());
why = check(value);
if (!why) {
accept(name, value, `Read ${f.what} from ${url}.`);
done(value);
const failed = [];
for (const t of tries) {
const url = new URL(t.url, location.href).href;
setRequest(card.querySelector('.request'), url, t.label);
setStatus(card.querySelector('.status'), 'pending', `Reading ${f.what}…`);
const read = await readJSON(url, check);
if (read.value) {
accept(name, read.value, `Read ${f.what} from ${url}${failed.length ? ` (${failed.join('; ')})` : ''}.`);
done(read.value);
return;
}
} catch (e) {
why = e instanceof SyntaxError ? 'it is not JSON' : e.message;
failed.push(`${url}: ${read.why}`);
}
setStatus(card.querySelector('.status'), 'error', `Could not read ${f.what}: ${why}.`);
setStatus(card.querySelector('.status'), 'error', `Could not read ${f.what}: ${failed.join('; ')}.`);
const input = h('input', { type: 'file', accept: '.json,application/json' });
input.addEventListener('change', async () => {
const file = input.files && input.files[0];
@@ -224,7 +247,7 @@
name === 'candidates'
? 'Without the candidates there is nothing to show. '
: 'Without the picks file the page cannot show what is picked, nor the whole file with a new pick. ',
'The page reads it relative to itself; served by the API only /examples/ is there. Load it from disk: ', input));
'Load it from disk: ', input));
}
function accept(name, value, text) {