Édition de la page d'accueil directement depuis YunoHost

- api.php : backend PHP de l'éditeur sur my_webapp (brouillon, publier,
  rétablir), réservé aux comptes listés dans .editors.json (EDITORS dans
  .env), identité lue dans l'en-tête YNH_USER du SSO, protection CSRF.
- preview.py répond aux mêmes routes api.php?action=..., l'éditeur n'a
  plus qu'un seul chemin de code ; Exporter n'existe qu'en local.
- Aperçu en direct dans l'éditeur (iframe + postMessage).
- Lien « Éditer la page d'accueil » dans le footer pour les éditeurs.
- Catalogue en ligne via /yunohost/portalapi/me.
- favicon.js : icône d'onglet = logo de l'app défini dans YunoHost.
- make deploy garde la config.json du serveur ; make deploy-config
  l'écrase après confirmation. DEPLOY_SSH permet de déployer via root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paraandClaude Opus 5.5 committed 2026-11-13 15:26:51 +01:00
1 parent d42141eb9f
commit a01d03aa09
14 files changed
+612 -48

No files matched your search

+37 -12
View File
@@ -9,6 +9,7 @@ import json
import os
import shutil
import sys
import urllib.parse
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
@@ -28,7 +29,12 @@ CONFIG_PATH = SRC_DIR / "config.json"
DRAFT_PATH = SRC_DIR / "config.draft.json"
WATCHED_FILES = ["index.html", "style.css", "app.js", "config.json", "config.draft.json"]
EXPORTED_FILES = ["index.html", "style.css", "app.js", "config.json"]
# config.json est exporte aussi, mais deploy.sh ne l'envoie que sur demande
# (make deploy-config) pour ne pas ecraser les modifs faites en ligne.
EXPORTED_FILES = [
"index.html", "style.css", "app.js", "config.json",
"editor.html", "editor.css", "editor.js", "api.php", "favicon.js",
]
RELOAD_SNIPPET = b"""
<script>
@@ -81,20 +87,27 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
self.wfile.write(body)
return
if self.path == "/__catalog":
self._send_catalog()
route, action = self._route()
if route == "/api.php":
if action == "whoami":
self._send_json({"user": "local", "canEdit": True, "local": True})
elif action == "catalog":
self._send_catalog()
elif action == "config":
draft = DRAFT_PATH.exists()
self._send_file(DRAFT_PATH if draft else CONFIG_PATH, draft)
elif action == "original":
self._send_file(CONFIG_PATH, False)
else:
self.send_error(404)
return
route = self.path.split("?", 1)[0]
# En local, l'apercu affiche le brouillon tant qu'il existe.
if route == "/config.json":
draft = DRAFT_PATH.exists()
self._send_file(DRAFT_PATH if draft else CONFIG_PATH, draft)
return
if route == "/__original":
self._send_file(CONFIG_PATH, False)
return
if self.path in ("/", "/index.html"):
try:
html = (SRC_DIR / "index.html").read_bytes()
@@ -111,11 +124,16 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
super().do_GET()
def do_POST(self):
if self.path in ("/__save", "/__draft"):
route, action = self._route()
if route != "/api.php":
self.send_error(404)
return
if action in ("save", "draft"):
data = self._read_json_body()
if data is None:
return
if self.path == "/__save":
if action == "save":
self._write_json(CONFIG_PATH, data)
DRAFT_PATH.unlink(missing_ok=True)
else:
@@ -123,12 +141,13 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
self._send_json({"ok": True})
return
if self.path == "/__discard":
if action == "discard":
DRAFT_PATH.unlink(missing_ok=True)
self._send_json({"ok": True})
return
if self.path == "/__export":
# Uniquement en local : sur le serveur, "Enregistrer" publie directement.
if action == "export":
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
copied = []
for name in EXPORTED_FILES:
@@ -141,6 +160,12 @@ class ReloadingHandler(http.server.SimpleHTTPRequestHandler):
self.send_error(404)
def _route(self):
"""Renvoie (chemin, action) ; action = parametre ?action= d'api.php."""
parsed = urllib.parse.urlsplit(self.path)
action = urllib.parse.parse_qs(parsed.query).get("action", [""])[0]
return parsed.path, action
def _send_catalog(self):
if os.environ.get("HOMEPAGE_DEMO"):
try: