import os import uvicorn from app import create_app, setup_routes from config.settings import PORT, OPENAI_API_KEY, BG_DATA_PATH,DEVELOPMENT, validate_config from logging import getLogger from threading import Thread from services.logging_service import structured_logger, LogLevel from services.data_service import initialize_db logger = getLogger("main") def main(): """Main application entry point""" logger.info("Starting Pedidos Express application") structured_logger.log_system_event( "Application startup initiated", LogLevel.INFO, { "port": PORT, "development_mode": DEVELOPMENT, "bg_data_path": BG_DATA_PATH } ) try: # Validate configuration if not validate_config(): logger.critical("FATAL: Configuration validation failed.") structured_logger.log_system_event( "Application startup failed: configuration validation error", LogLevel.CRITICAL, {"validation_step": "configuration"} ) if not OPENAI_API_KEY: logger.error("Please create a .env file with OPENAI_API_KEY='your_key_here'") with open(".env", "w") as f: f.write("OPENAI_API_KEY='your_key_here'") return None structured_logger.log_system_event( "Configuration validation successful", LogLevel.INFO ) # Create and configure app logger.info("Creating FastAPI application") app = create_app() logger.info("Setting up application routes") setup_routes(app) structured_logger.log_system_event( "FastAPI application created and routes configured", LogLevel.INFO ) # Initialize database logger.info("Initializing database") initialize_db() structured_logger.log_system_event( "Database initialization completed", LogLevel.INFO ) # Display startup information logger.info(f"Server starting on http://localhost:{PORT}") if not os.path.exists(BG_DATA_PATH): logger.warning(f"WARNING: {BG_DATA_PATH} not found. AI assistant will not have specific menu data.") structured_logger.log_system_event( "AI assistant data file not found", LogLevel.WARNING, { "bg_data_path": BG_DATA_PATH, "impact": "AI assistant will have limited functionality" } ) else: logger.info(f"AI assistant data loaded from: {os.path.abspath(BG_DATA_PATH)}") structured_logger.log_system_event( "AI assistant data loaded successfully", LogLevel.INFO, {"bg_data_path": os.path.abspath(BG_DATA_PATH)} ) structured_logger.log_system_event( "Application startup completed successfully", LogLevel.INFO, { "server_url": f"http://localhost:{PORT}", "development_mode": DEVELOPMENT } ) logger.info("Pedidos Express application ready to serve requests") return app except Exception as e: error_msg = f"Critical error during application startup: {e}" logger.critical(error_msg) structured_logger.log_system_event( "Application startup failed with critical error", LogLevel.CRITICAL, { "error": str(e), "error_type": type(e).__name__ } ) return None app = main() if not app: logger.critical("FATAL: Failed to create FastAPI app.") structured_logger.log_system_event( "Application failed to start - FastAPI app creation failed", LogLevel.CRITICAL ) exit(1) logger.info("Application initialized successfully") if __name__ == "__main__": logger.info(f"Starting server with uvicorn on port {PORT}") structured_logger.log_system_event( "Starting uvicorn server", LogLevel.INFO, { "host": "0.0.0.0", "port": PORT, "log_level": "info" } ) uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info", access_log=False)