AccroCBio/start.sh
Louis Mazin 4fd1c14d1d test
2026-10-15 16:45:02 +02:00

118 lines
3.5 KiB
Bash

#!/bin/bash
set -Eeuo pipefail
cd /home/container
echo "======================================"
echo "AccroCBio"
echo "======================================"
# --------------------------------------
# Configuration .env (sans `source` : mots de passe avec caractères spéciaux)
# --------------------------------------
read_env() {
local key="$1" value
value="$(sed -n "s/^${key}=//p" /home/container/.env 2>/dev/null | tail -n 1 | tr -d '\r' || true)"
value="${value#\"}"
value="${value%\"}"
value="${value#\'}"
value="${value%\'}"
printf '%s' "$value"
}
# --------------------------------------
# Mise à jour du projet (comme le start.sh fourni)
# --------------------------------------
if [[ ! -d .git ]]; then
if [[ -f backend/manage.py && -f frontend/package.json ]]; then
echo "Projet présent sans .git : démarrage sans clonage."
else
REPOSITORY_URL="$(read_env REPOSITORY_URL)"
REPOSITORY_URL="${REPOSITORY_URL:-https://gitea.louismazin.ovh/LouisMazin/AccroCBio.git}"
if [[ -z "$REPOSITORY_URL" ]]; then
echo "Dépôt Git absent : ajoute REPOSITORY_URL au .env ou copie le projet." >&2
exit 1
fi
echo "Dépôt Git absent → clonage..."
git clone "$REPOSITORY_URL" /tmp/accrocbio-site
cp -a /tmp/accrocbio-site/. /home/container/
rm -rf /tmp/accrocbio-site
fi
else
echo "Mise à jour du dépôt Git..."
git fetch origin
git reset --hard origin/main
fi
if [[ -d .git ]]; then
echo "Git : $(git rev-parse --short HEAD)"
fi
FRONTEND_PORT="$(read_env FRONTEND_PORT)"
BACKEND_PORT="$(read_env BACKEND_PORT)"
for variable in FRONTEND_PORT BACKEND_PORT; do
value="${!variable}"
if ! [[ "$value" =~ ^[1-9][0-9]{0,4}$ ]] || (( value > 65535 )); then
echo "Port $variable absent ou incorrect dans .env (attendu : 1 à 65535)." >&2
exit 1
fi
done
if [[ "$FRONTEND_PORT" == "$BACKEND_PORT" ]]; then
echo "Les ports FRONTEND_PORT et BACKEND_PORT doivent être différents." >&2
exit 1
fi
export FRONTEND_PORT BACKEND_PORT
# Sous Pterodactyl, les deux applications tournent dans le même conteneur.
export BACKEND_PROXY_HOST=127.0.0.1
# --------------------------------------
# Backend
# --------------------------------------
cd /home/container/backend
python3 manage.py check
python3 manage.py migrate --noinput
python3 manage.py ensure_admin
python3 manage.py collectstatic --noinput --verbosity 0
echo "Démarrage Django sur :${BACKEND_PORT}"
python3 manage.py runserver "0.0.0.0:${BACKEND_PORT}" --noreload &
BACKEND_PID=$!
# --------------------------------------
# Frontend
# --------------------------------------
cd /home/container/frontend
echo "Installation des dépendances frontend..."
if [[ -f package-lock.json ]]; then
npm ci
else
npm install
fi
echo "Build React..."
npm run build
echo "Démarrage frontend sur :${FRONTEND_PORT}"
npm run preview -- --host 0.0.0.0 --port "$FRONTEND_PORT" --strictPort &
FRONTEND_PID=$!
# --------------------------------------
# Arrêt propre
# --------------------------------------
cleanup() {
trap - SIGTERM SIGINT EXIT
echo "Arrêt des applications..."
kill "$BACKEND_PID" "$FRONTEND_PID" 2>/dev/null || true
wait "$BACKEND_PID" "$FRONTEND_PID" 2>/dev/null || true
}
trap cleanup SIGTERM SIGINT EXIT
echo "======================================"
echo " Frontend : :${FRONTEND_PORT}"
echo " Backend : :${BACKEND_PORT}"
echo "======================================"
wait -n "$BACKEND_PID" "$FRONTEND_PID"