File size: 2,442 Bytes
d543fc1
 
 
 
 
 
 
7bdbd3c
0d58169
 
 
d543fc1
 
7bdbd3c
d543fc1
 
 
 
 
 
 
 
 
 
 
 
 
2b80190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d543fc1
 
 
 
2b80190
 
d543fc1
 
71b0b04
 
 
 
 
 
 
 
d543fc1
 
 
7bdbd3c
d543fc1
7bdbd3c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sys
from dotenv import load_dotenv

# Ensure environment variables are loaded
load_dotenv()

# Insert the backend module into python path so internal absolute imports work
root_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(root_dir, 'backend'))
sys.path.insert(0, root_dir)

# Import the application factory from our partitioned architecture
from backend 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('/uploads/<path:filename>')
def serve_root_uploads(filename):
    """Serve uploaded logos and static media files directly from uploads directory."""
    uploads_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads')
    file_path = os.path.join(uploads_dir, filename)
    if os.path.exists(file_path):
        return send_from_directory(uploads_dir, filename)
    
    # Check inside uploads/logos subfolder if requested without 'logos/' prefix
    logos_dir = os.path.join(uploads_dir, 'logos')
    if os.path.exists(os.path.join(logos_dir, filename)):
        return send_from_directory(logos_dir, filename)
        
    abort(404)


@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)):
        res = send_from_directory(FRONTEND_DIR, path)
    else:
        res = send_from_directory(FRONTEND_DIR, 'index.html')

    res.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    res.headers['Pragma'] = 'no-cache'
    res.headers['Expires'] = '0'
    return res


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