É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:
1 parent
d42141eb9f
commit
a01d03aa09
14 files changed
+612
-48
No files matched your search
+56
-7
@@ -37,7 +37,7 @@ REMOTE_PATH="${REMOTE_PATH:-/www/}"
|
||||
|
||||
SCP="scp"
|
||||
|
||||
if [ -n "${REMOTE_PASSWORD:-}" ]; then
|
||||
if [ -n "${REMOTE_PASSWORD:-}" ] && [ -z "${DEPLOY_SSH:-}" ]; then
|
||||
if ! command -v sshpass > /dev/null 2>&1; then
|
||||
echo "REMOTE_PASSWORD est defini dans .env mais sshpass n'est pas installe (apt install sshpass)." >&2
|
||||
exit 1
|
||||
@@ -45,9 +45,58 @@ if [ -n "${REMOTE_PASSWORD:-}" ]; then
|
||||
SCP="sshpass -p ${REMOTE_PASSWORD} scp"
|
||||
fi
|
||||
|
||||
${SCP} -P "${REMOTE_PORT}" \
|
||||
"${OUTPUT_DIR}/index.html" \
|
||||
"${OUTPUT_DIR}/style.css" \
|
||||
"${OUTPUT_DIR}/app.js" \
|
||||
"${OUTPUT_DIR}/config.json" \
|
||||
"${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
|
||||
for f in editor.html editor.css editor.js api.php favicon.js; do
|
||||
if [ ! -f "${OUTPUT_DIR}/$f" ]; then
|
||||
echo "output/$f manquant : relance 'make export'." >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
FILES="index.html style.css app.js editor.html editor.css editor.js api.php favicon.js"
|
||||
|
||||
# Liste des comptes YunoHost autorises a modifier la page en ligne
|
||||
# (EDITORS dans .env, separes par des espaces ou des virgules). Le fichier
|
||||
# commence par un point : nginx refuse de le servir, seul api.php le lit.
|
||||
EDITORS_LIST="$(printf '%s' "${EDITORS:-}" | tr ',' ' ')"
|
||||
if [ -n "$(printf '%s' "$EDITORS_LIST" | tr -d ' ')" ]; then
|
||||
json="["
|
||||
sep=""
|
||||
for u in $EDITORS_LIST; do
|
||||
case "$u" in
|
||||
*[!a-z0-9._-]*) echo "Nom d'utilisateur invalide dans EDITORS : $u" >&2; exit 1 ;;
|
||||
esac
|
||||
json="${json}${sep}\"${u}\""
|
||||
sep=", "
|
||||
done
|
||||
printf '%s]\n' "$json" > "${OUTPUT_DIR}/.editors.json"
|
||||
FILES="$FILES .editors.json"
|
||||
else
|
||||
echo "EDITORS vide dans .env : liste des editeurs non envoyee (personne ne pourra editer en ligne si elle n'existe pas deja)." >&2
|
||||
fi
|
||||
|
||||
# config.json n'est envoye que sur demande explicite (make deploy-config) :
|
||||
# sinon on garderait le risque d'ecraser les modifications faites en ligne
|
||||
# depuis l'editeur. Pour recuperer la version du serveur : make fetch.
|
||||
if [ "${DEPLOY_CONFIG:-0}" = "1" ]; then
|
||||
FILES="$FILES config.json"
|
||||
echo "config.json locale envoyee : elle REMPLACE celle du serveur."
|
||||
else
|
||||
echo "config.json du serveur conservee (make deploy-config pour l'ecraser)."
|
||||
fi
|
||||
|
||||
set --
|
||||
for f in $FILES; do
|
||||
set -- "$@" "${OUTPUT_DIR}/$f"
|
||||
done
|
||||
|
||||
if [ -n "${DEPLOY_SSH:-}" ]; then
|
||||
# Via un compte root (ex. root@mondomaine ou un alias SSH) : chemin absolu du serveur,
|
||||
# puis les fichiers sont rendus au compte de l'app pour que PHP (qui tourne
|
||||
# sous ce compte) puisse reecrire config.json et le brouillon.
|
||||
REMOTE_DIR="/var/www/${REMOTE_USER}/$(printf '%s' "$REMOTE_PATH" | sed 's#^/##')"
|
||||
scp "$@" "${DEPLOY_SSH}:${REMOTE_DIR}"
|
||||
ssh "${DEPLOY_SSH}" "cd '${REMOTE_DIR}' && chown '${REMOTE_USER}:${REMOTE_USER}' ${FILES} && chmod 664 ${FILES} && { [ ! -f .editors.json ] || chmod 640 .editors.json; }"
|
||||
else
|
||||
${SCP} -P "${REMOTE_PORT}" "$@" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
|
||||
fi
|
||||
echo "Deploye : $FILES"
|
||||
+4
-1
@@ -10,7 +10,10 @@ SRC_DIR="$PROJECT_ROOT/src"
|
||||
OUTPUT_DIR="$PROJECT_ROOT/output"
|
||||
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
cp "$SRC_DIR/index.html" "$SRC_DIR/style.css" "$SRC_DIR/app.js" "$SRC_DIR/config.json" "$OUTPUT_DIR/"
|
||||
# Meme liste que EXPORTED_FILES dans scripts/preview.py.
|
||||
for f in index.html style.css app.js config.json editor.html editor.css editor.js api.php favicon.js; do
|
||||
cp "$SRC_DIR/$f" "$OUTPUT_DIR/"
|
||||
done
|
||||
|
||||
echo "Exporte vers $OUTPUT_DIR :"
|
||||
ls "$OUTPUT_DIR"
|
||||
+37
-12
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user