Simplify path resolution for static files in Flask app

This commit is contained in:
Jarian Cottingham 2026-02-02 08:20:21 -06:00
parent 70c0106181
commit 4c7b60b99a

View File

@ -5,9 +5,6 @@ import os
app = Flask(__name__) app = Flask(__name__)
CORS(app) # Enable CORS for all routes CORS(app) # Enable CORS for all routes
# Get the directory where this script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
# Import our archive parser # Import our archive parser
from parse_archive import ArchiveParser from parse_archive import ArchiveParser
@ -16,18 +13,20 @@ archive_parser = ArchiveParser()
# Serve static files from the website directory # Serve static files from the website directory
# The website directory is at the same level as the src directory
@app.route("/<path:filename>") @app.route("/<path:filename>")
def serve_static(filename): def serve_static(filename):
# Use absolute path to website directory # In Docker, the structure is: /app/src/server/ and /app/website/
website_dir = os.path.join(script_dir, "..", "website") # So we need to go up from src/server to /app/ and then to /app/website
website_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "website")
return send_from_directory(website_dir, filename) return send_from_directory(website_dir, filename)
# Serve the main index.html page # Serve the main index.html page
@app.route("/") @app.route("/")
def serve_index(): def serve_index():
# Use absolute path to website directory # In Docker, the structure is: /app/src/server/ and /app/website/
website_dir = os.path.join(script_dir, "..", "website") website_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "website")
return send_from_directory(website_dir, "index.html") return send_from_directory(website_dir, "index.html")