forked from tpd94/CDRM-Project
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""Module to handle the React routes."""
|
|
|
|
import os
|
|
import sys
|
|
|
|
from flask import Blueprint, send_from_directory, render_template
|
|
from configs import index_tags
|
|
|
|
if getattr(sys, "frozen", False): # Running as a bundled app
|
|
base_path = getattr(sys, "_MEIPASS", os.path.abspath("."))
|
|
else: # Running in a normal Python environment
|
|
base_path = os.path.abspath(".")
|
|
|
|
static_folder = os.path.join(base_path, "frontend-dist")
|
|
|
|
react_bp = Blueprint(
|
|
"react_bp",
|
|
__name__,
|
|
static_folder=static_folder,
|
|
static_url_path="/",
|
|
template_folder=static_folder,
|
|
)
|
|
|
|
|
|
@react_bp.route("/", methods=["GET"])
|
|
@react_bp.route("/<path:path>", methods=["GET"])
|
|
@react_bp.route("/<path>", methods=["GET"])
|
|
def index(path=""):
|
|
"""Handle the index route."""
|
|
# Ensure static_folder is not None
|
|
if react_bp.static_folder is None:
|
|
raise ValueError("Static folder is not configured for the blueprint")
|
|
|
|
# Normalize the path to prevent directory traversal
|
|
safe_path = os.path.normpath(path)
|
|
file_path = os.path.join(react_bp.static_folder, safe_path)
|
|
|
|
if path and os.path.exists(file_path):
|
|
return send_from_directory(react_bp.static_folder, safe_path)
|
|
|
|
# Only allow certain paths to render index.html with tags
|
|
allowed_paths = ["", "cache", "api", "testplayer", "account"]
|
|
if safe_path.lower() in allowed_paths:
|
|
data = index_tags.tags.get(safe_path.lower(), index_tags.tags.get("index", {}))
|
|
return render_template("index.html", data=data)
|
|
|
|
# Fallback: serve index.html for all other routes (SPA)
|
|
return send_from_directory(react_bp.static_folder, "index.html")
|