1 changed files with 41 additions and 0 deletions
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations |
||||
|
||||
import json |
||||
import secrets |
||||
from pathlib import Path |
||||
|
||||
from flask import Flask |
||||
|
||||
from .routes import ui |
||||
|
||||
|
||||
def _load_secret_key(conf_path: Path) -> str: |
||||
default_key = secrets.token_hex(32) |
||||
|
||||
if not conf_path.exists(): |
||||
return default_key |
||||
|
||||
try: |
||||
payload = json.loads(conf_path.read_text(encoding="utf-8")) |
||||
except Exception: |
||||
return default_key |
||||
|
||||
value = payload.get("secret_key") if isinstance(payload, dict) else None |
||||
if isinstance(value, str) and value.strip(): |
||||
return value.strip() |
||||
return default_key |
||||
|
||||
|
||||
def create_app() -> Flask: |
||||
app = Flask(__name__) |
||||
|
||||
frontend_root = Path(__file__).resolve().parents[1] |
||||
project_root = Path(__file__).resolve().parents[2] |
||||
conf_path = frontend_root / "data" / "conf.json" |
||||
|
||||
app.config["PROJECT_ROOT"] = project_root |
||||
app.config["MUSIC_DIR"] = project_root / "backend" / "data" / "musiques" |
||||
app.secret_key = _load_secret_key(conf_path) |
||||
|
||||
app.register_blueprint(ui) |
||||
return app |
||||
Loading…
Reference in new issue