Fichiers
site-principal/deploy/appliquer-caddy-build.sh
T

48 lignes
1.5 KiB
Bash

#!/bin/bash
# Phase 1 perf gastinel.me : /assets/build/* (fichiers a empreinte produits par
# tools/build.js) -> cache 1 an immutable + .br/.gz precompresses.
# Ne fait qu'AJOUTER un handle dans le bloc "gastinel.me, www.gastinel.me".
# Sauvegarde -> modification -> caddy validate -> reload ; retour arriere si echec.
# Usage (sur cornafsuisse) : sudo bash ~/appliquer-caddy-build.sh
set -euo pipefail
[ "$(id -u)" = 0 ] || { echo "A lancer avec sudo"; exit 1; }
CF=/etc/caddy/Caddyfile
BAK="$CF.avant-build-$(date +%Y%m%d-%H%M%S)"
cp -a "$CF" "$BAK"
echo "Sauvegarde : $BAK"
python3 - "$CF" <<'PY'
import sys
p = sys.argv[1]
s = open(p).read()
if "handle /assets/build/*" in s:
sys.exit("Deja applique : aucune modification.")
anchor = "\t# Assets (polices, images) : cache navigateur 30 jours.\n"
new = (
"\t# Assets a empreinte (tools/build.js) : le nom change avec le contenu,\n"
"\t# donc cache 1 an sans revalidation.\n"
"\thandle /assets/build/* {\n"
"\t\theader Cache-Control \"public, max-age=31536000, immutable\"\n"
"\t\troot * /srv/gastinel.me\n"
"\t\tfile_server {\n"
"\t\t\tprecompressed br gzip\n"
"\t\t}\n"
"\t}\n"
"\n"
)
n = s.count(anchor)
if n != 1:
sys.exit("Point d'insertion introuvable ou ambigu (%d) : aucune modification." % n)
open(p, "w").write(s.replace(anchor, new + anchor))
PY
if caddy validate --config "$CF" --adapter caddyfile && systemctl reload caddy; then
echo "OK : Caddy recharge."
else
echo "ECHEC : restauration de $BAK"
cp -a "$BAK" "$CF"
systemctl reload caddy || true
exit 1
fi