veroeffentlichen.sh baut mit werkzeuge/pages_bauen.py den Ordner .pages und laedt ihn nach hivegrid-marketing.pages.dev. Die Middleware in werkzeuge/pages/ uebernimmt Passwort, nur lesen und die Auskuenfte von serve.py (Dokumente, Stand, Suche). Der Tunnel bleibt als Notloesung. Anleitung in .docs/Freigabe.md, Hinweis fuer Agents in .claude/CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.7 KiB
JavaScript
94 lines
3.7 KiB
JavaScript
// Cloudflare Pages: das Dashboard für Kollegen, ohne Dejans Laptop.
|
|
//
|
|
// Läuft vor jeder Anfrage und macht, was serve.py und freigabe.py lokal tun:
|
|
// - jede Anfrage braucht das Passwort (Pages-Secret PASSWORT), der Name ist egal
|
|
// - nur lesen: Status setzen, Export und Öffnen antworten mit 403
|
|
// - "/" zeigt das Dashboard
|
|
// - /api/dokumente, /api/stand und /api/suche kommen aus festen Dateien in
|
|
// pages-daten/, die werkzeuge/pages_bauen.py beim Hochladen erzeugt
|
|
//
|
|
// Hochgeladen wird mit ./veroeffentlichen.sh, Anleitung in .docs/Freigabe.md.
|
|
|
|
const NUR_LESEN = { fehler: "In der Freigabe lässt sich nichts ändern." };
|
|
|
|
export async function onRequest({ request, env, next }) {
|
|
if (!erlaubt(request, env.PASSWORT)) {
|
|
return new Response("Passwort nötig", {
|
|
status: 401,
|
|
headers: { "WWW-Authenticate": 'Basic realm="HiveGrid Marketing", charset="UTF-8"' },
|
|
});
|
|
}
|
|
const url = new URL(request.url);
|
|
if (request.method !== "GET" && request.method !== "HEAD") return json(403, NUR_LESEN);
|
|
|
|
if (url.pathname === "/" || url.pathname === "/index.html") return asset(env, url, "/Dashboard/");
|
|
if (url.pathname === "/api/dokumente") return asset(env, url, "/pages-daten/dokumente.json");
|
|
if (url.pathname === "/api/stand") return asset(env, url, "/pages-daten/stand.json");
|
|
if (url.pathname === "/api/suche") return suche(env, url);
|
|
|
|
const antwort = await next();
|
|
// Markdown als Text mit Umlauten, so wie serve.py es ausliefert.
|
|
if (url.pathname.endsWith(".md") && antwort.ok) {
|
|
const neu = new Response(antwort.body, antwort);
|
|
neu.headers.set("Content-Type", "text/markdown; charset=utf-8");
|
|
return neu;
|
|
}
|
|
return antwort;
|
|
}
|
|
|
|
function erlaubt(request, passwort) {
|
|
if (!passwort) return false;
|
|
const kopf = request.headers.get("Authorization") || "";
|
|
if (!kopf.startsWith("Basic ")) return false;
|
|
let pw = "";
|
|
try {
|
|
const roh = Uint8Array.from(atob(kopf.slice(6)), (z) => z.charCodeAt(0));
|
|
const text = new TextDecoder().decode(roh);
|
|
pw = text.slice(text.indexOf(":") + 1);
|
|
} catch {
|
|
return false;
|
|
}
|
|
// Immer alle Zeichen vergleichen, damit die Antwortzeit nichts verrät.
|
|
let unterschied = pw.length ^ passwort.length;
|
|
for (let i = 0; i < passwort.length; i++) unterschied |= (pw.charCodeAt(i) || 0) ^ passwort.charCodeAt(i);
|
|
return unterschied === 0;
|
|
}
|
|
|
|
async function asset(env, url, pfad) {
|
|
const antwort = await env.ASSETS.fetch(new URL(pfad, url));
|
|
const neu = new Response(antwort.body, antwort);
|
|
neu.headers.set("Cache-Control", "no-store");
|
|
return neu;
|
|
}
|
|
|
|
// Wie _suche in serve.py: Zeile für Zeile, ohne Groß- und Kleinschreibung,
|
|
// mit der letzten Überschrift davor, höchstens 300 Treffer.
|
|
async function suche(env, url) {
|
|
const q = (url.searchParams.get("q") || "").trim();
|
|
if (q.length < 2) return json(200, []);
|
|
const index = await (await env.ASSETS.fetch(new URL("/pages-daten/suche.json", url))).json();
|
|
const gesucht = q.toLowerCase();
|
|
const treffer = [];
|
|
for (const dok of index) {
|
|
let ueberschrift = "";
|
|
for (let i = 0; i < dok.zeilen.length; i++) {
|
|
const zeile = dok.zeilen[i];
|
|
if (zeile.startsWith("#")) ueberschrift = zeile.replace(/^#+/, "").trim();
|
|
const pos = zeile.toLowerCase().indexOf(gesucht);
|
|
if (pos < 0) continue;
|
|
const a = Math.max(0, pos - 70);
|
|
const text = (a ? "…" : "") + zeile.slice(a, pos + q.length + 90).trim();
|
|
treffer.push({ pfad: dok.pfad, titel: dok.titel, zeile: i + 1, ueberschrift, text });
|
|
if (treffer.length >= 300) return json(200, treffer);
|
|
}
|
|
}
|
|
return json(200, treffer);
|
|
}
|
|
|
|
function json(status, daten) {
|
|
return new Response(JSON.stringify(daten), {
|
|
status,
|
|
headers: { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" },
|
|
});
|
|
}
|