import os import secrets from flask import Flask _BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) _TEMPLATE_FOLDER = os.path.join(_BASE_DIR, "templates") _STATIC_FOLDER = os.path.join(_BASE_DIR, "static") app = Flask(__name__, template_folder=_TEMPLATE_FOLDER, static_folder=_STATIC_FOLDER) def _load_or_create_secret_key(): """Nécessaire dès qu'une session Flask (flask.session) est utilisée — ici pour se souvenir de qui est connecté (auth/). Généré une seule fois et PERSISTÉ dans un fichier (jamais en dur dans le code, jamais commité — voir .gitignore, data/) : sans ça, chaque redémarrage du serveur changerait la clé et déconnecterait tout le monde. Le chemin est overridable (FORGE_SECRET_KEY_PATH) pour que les tests utilisent un fichier temporaire, jamais celui du poste réel.""" path = os.environ.get("FORGE_SECRET_KEY_PATH") or os.path.join(_BASE_DIR, "data", "secret_key") if os.path.isfile(path): with open(path, "r", encoding="utf-8") as f: key = f.read().strip() if key: return key key = secrets.token_hex(32) os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w", encoding="utf-8") as f: f.write(key) return key app.secret_key = _load_or_create_secret_key()