File size: 1,526 Bytes
d543fc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import sys
from dotenv import load_dotenv

# Ensure environment variables are loaded
load_dotenv()

# Insert the backend module into the python path so its internal absolute imports work
sys.path.insert(0, os.path.abspath('backend_structured'))

# Import the application factory from our partitioned architecture
from backend_structured import create_app

# Initialize the Flask application
app = create_app()

# ---------------------------------------------------------------------------
# Serve the built React frontend (HF Spaces / single-container mode)
# In development the Vite dev-server proxies /api/ to Flask instead.
# ---------------------------------------------------------------------------
from flask import send_from_directory, abort

FRONTEND_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'frontend', 'dist')


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve_react(path):
    """Serve React SPA. Static assets go to dist/, everything else → index.html."""
    # Never intercept API or socket.io requests
    if path.startswith('api/') or path.startswith('socket.io'):
        abort(404)
    if path and os.path.exists(os.path.join(FRONTEND_DIR, path)):
        return send_from_directory(FRONTEND_DIR, path)
    return send_from_directory(FRONTEND_DIR, 'index.html')


if __name__ == '__main__':
    from backend_structured.extensions import socketio
    port = int(os.getenv('PORT', 7860))
    socketio.run(app, host='0.0.0.0', port=port, debug=True)