gaialive commited on
Commit
804e111
·
verified ·
1 Parent(s): 9c052f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -7
app.py CHANGED
@@ -181,7 +181,24 @@ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max upload
181
  app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
182
 
183
  # Initialize Socket.IO for real-time communication
184
- socketio = SocketIO(app, async_mode='eventlet', cors_allowed_origins="*")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
  def timeout(seconds):
187
  """
@@ -871,6 +888,19 @@ def index():
871
  # Serve React index.html
872
  return send_from_directory(app.static_folder, 'index.html')
873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
874
  @app.route('/sitemap.xml')
875
  def sitemap():
876
  """Generate the sitemap.xml file dynamically."""
@@ -1447,10 +1477,22 @@ def server_error(e):
1447
 
1448
  # --- Main entry point ---
1449
  if __name__ == "__main__":
1450
- # SSL Configuration for production
1451
- if os.environ.get('FLASK_ENV') == 'production' and os.path.exists('cert.pem') and os.path.exists('key.pem'):
1452
- socketio.run(app, debug=False, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)),
1453
- certfile='cert.pem', keyfile='key.pem')
 
 
 
 
 
 
 
1454
  else:
1455
- # Development environment
1456
- socketio.run(app, debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
 
 
 
 
 
 
181
  app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
182
 
183
  # Initialize Socket.IO for real-time communication
184
+ # Use environment variable to determine if SocketIO should be enabled
185
+ socketio_enabled = os.environ.get('SOCKETIO_ENABLED', 'true').lower() == 'true'
186
+
187
+ if socketio_enabled:
188
+ socketio = SocketIO(app, async_mode='eventlet', cors_allowed_origins="*", logger=True, engineio_logger=True)
189
+ else:
190
+ # Create a mock socketio object for environments where SocketIO is not supported
191
+ class MockSocketIO:
192
+ def on(self, *args, **kwargs):
193
+ def decorator(func):
194
+ return func
195
+ return decorator
196
+ def emit(self, *args, **kwargs):
197
+ pass
198
+ def run(self, *args, **kwargs):
199
+ pass
200
+
201
+ socketio = MockSocketIO()
202
 
203
  def timeout(seconds):
204
  """
 
888
  # Serve React index.html
889
  return send_from_directory(app.static_folder, 'index.html')
890
 
891
+ @app.route("/health")
892
+ def health():
893
+ """Health check endpoint for Hugging Face Spaces"""
894
+ return jsonify({"status": "healthy", "timestamp": datetime.now().isoformat()}), 200
895
+
896
+ @app.route("/favicon.ico")
897
+ def favicon():
898
+ """Favicon endpoint"""
899
+ try:
900
+ return send_from_directory(app.static_folder, 'favicon.ico')
901
+ except Exception:
902
+ return "", 204
903
+
904
  @app.route('/sitemap.xml')
905
  def sitemap():
906
  """Generate the sitemap.xml file dynamically."""
 
1477
 
1478
  # --- Main entry point ---
1479
  if __name__ == "__main__":
1480
+ # Check if we're running in a containerized environment
1481
+ is_containerized = os.environ.get('CONTAINERIZED', 'false').lower() == 'true'
1482
+ port = int(os.environ.get('PORT', 5000))
1483
+ host = os.environ.get('HOST', '127.0.0.1')
1484
+
1485
+ if is_containerized or os.environ.get('FLASK_ENV') == 'production':
1486
+ # In containerized environments, use standard Flask development server
1487
+ # This is more compatible with platforms like Hugging Face Spaces
1488
+ from werkzeug.serving import run_simple
1489
+ print(f"Starting Flask app on {host}:{port} (containerized mode)")
1490
+ app.run(host=host, port=port, debug=False)
1491
  else:
1492
+ # SSL Configuration for development
1493
+ if os.environ.get('FLASK_ENV') == 'production' and os.path.exists('cert.pem') and os.path.exists('key.pem'):
1494
+ socketio.run(app, debug=False, host=host, port=port,
1495
+ certfile='cert.pem', keyfile='key.pem')
1496
+ else:
1497
+ # Development environment with SocketIO
1498
+ socketio.run(app, debug=True, host=host, port=port)