bettersight-statement-extra.../backend/app.py

62 lines
2.3 KiB
Python

# Flask app factory - registers blueprints, wires config from dependencies.py.
import os
from flask import Flask, send_from_directory
import dependencies
from routes.extraction import extraction_bp
from routes.health import health_bp
FRONTEND_DIST = os.path.join(os.path.dirname(__file__), "..", "frontend", "dist")
def create_app():
"""
Builds and configures the Flask application.
Data flow:
(no input)
returns (Flask) - the configured app, routes registered, ready to run
Steps:
1. Create the Flask instance, static files served from the built
Vue app (frontend/dist, produced by `npm run build`) rather than
Jinja-rendered templates - Stage 2 of the enterprise-standards
migration moved the frontend to its own Vue 3 app; templates/
is no longer used
2. Apply config from dependencies.py (upload size cap)
3. Register the extraction and health blueprints
4. "/" and any other non-API path serve the SPA's own index.html
(Vite's own client-side router has just the one route today,
but this keeps a future deep link or page refresh working) -
Flask's static route only matches an EXISTING file under
frontend/dist (e.g. /assets/index-XXXX.js), so this catch-all
is what serves index.html for everything else
"""
# static_folder=None - Flask's own automatic static route would
# register at the same "/<path:filename>" pattern as the manual
# catch-all below, a duplicate/conflicting route registration.
# Serving is handled entirely by that catch-all instead.
app = Flask(__name__, static_folder=None)
app.config["MAX_CONTENT_LENGTH"] = dependencies.MAX_CONTENT_LENGTH
app.register_blueprint(extraction_bp)
app.register_blueprint(health_bp)
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def spa(path):
full_path = os.path.join(FRONTEND_DIST, path)
if path and os.path.isfile(full_path):
return send_from_directory(FRONTEND_DIST, path)
return send_from_directory(FRONTEND_DIST, "index.html")
return app
app = create_app()
if __name__ == "__main__":
os.makedirs(dependencies.UPLOAD_FOLDER, exist_ok=True)
port = int(os.environ.get("PORT", 8080))
app.run(host="0.0.0.0", port=port, debug=False, threaded=True)