Explorar el Código

Fix: Frontend

iaun hace 3 meses
padre
commit
b6b0fef883
Se han modificado 1 ficheros con 19 adiciones y 2 borrados
  1. 19 2
      backend/app/__init__.py

+ 19 - 2
backend/app/__init__.py

@@ -1,5 +1,6 @@
 """Flask application factory for Work Statistics System."""
-from flask import Flask
+import os
+from flask import Flask, send_from_directory
 from flask_sqlalchemy import SQLAlchemy
 from flask_cors import CORS
 
@@ -15,7 +16,12 @@ def create_app(config_name='default'):
     Returns:
         Configured Flask application instance
     """
-    app = Flask(__name__)
+    # Check if static folder exists (for production with frontend build)
+    static_folder = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static')
+    if os.path.exists(static_folder):
+        app = Flask(__name__, static_folder=static_folder, static_url_path='')
+    else:
+        app = Flask(__name__)
     
     # Load configuration
     from app.config import config
@@ -29,6 +35,17 @@ def create_app(config_name='default'):
     from app.routes import register_routes
     register_routes(app)
     
+    # Serve frontend for non-API routes (SPA support)
+    if app.static_folder and os.path.exists(app.static_folder):
+        @app.route('/')
+        def serve_index():
+            return send_from_directory(app.static_folder, 'index.html')
+        
+        @app.errorhandler(404)
+        def not_found(e):
+            # Return index.html for SPA client-side routing
+            return send_from_directory(app.static_folder, 'index.html')
+    
     # Create database tables and initialize default admin
     with app.app_context():
         db.create_all()