Production Environment Fixes: - Enhanced SocketIO configuration for proxy compatibility - Added fallback polling mechanism when WebSocket fails - Fixed environment configuration (FLASK_ENV vs FLASK_CONFIG) - Added production-friendly debug logging for distribution chart - Improved connection status monitoring and error handling Proxy-Specific Improvements: - Enhanced CORS and transport settings for SocketIO - Fallback to HTTP polling when WebSocket connections fail - Better error handling and retry mechanisms - Debug logging that works in production mode This should resolve the wealth distribution histogram issue when running behind Nginx proxy in Docker containers.
58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
"""
|
|
Flask Application Factory
|
|
|
|
Creates and configures the Flask application with SocketIO support for real-time
|
|
updates of the Markov economics simulation.
|
|
"""
|
|
|
|
import os
|
|
from flask import Flask
|
|
from flask_socketio import SocketIO
|
|
from config import config
|
|
from app.models import SimulationManager
|
|
|
|
# Initialize SocketIO with enhanced proxy support
|
|
socketio = SocketIO(
|
|
cors_allowed_origins="*",
|
|
async_mode='threading',
|
|
logger=True,
|
|
engineio_logger=True
|
|
)
|
|
|
|
# Global simulation manager instance
|
|
simulation_manager = SimulationManager()
|
|
|
|
|
|
def create_app(config_name=None):
|
|
"""
|
|
Create and configure the Flask application.
|
|
|
|
Args:
|
|
config_name: Configuration profile to use
|
|
|
|
Returns:
|
|
Configured Flask application instance
|
|
"""
|
|
if config_name is None:
|
|
config_name = os.getenv('FLASK_CONFIG', os.getenv('FLASK_ENV', 'development'))
|
|
|
|
app = Flask(__name__)
|
|
app.config.from_object(config[config_name])
|
|
|
|
# Initialize extensions with proxy-friendly settings
|
|
socketio.init_app(
|
|
app,
|
|
async_mode='threading',
|
|
cors_allowed_origins="*",
|
|
allow_upgrades=True,
|
|
transports=['websocket', 'polling']
|
|
)
|
|
|
|
# Register blueprints
|
|
from .routes.main import main_bp
|
|
from .routes.api import api_bp
|
|
|
|
app.register_blueprint(main_bp)
|
|
app.register_blueprint(api_bp, url_prefix='/api')
|
|
|
|
return app |