main.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/home/superti/miniconda3/envs/pedidos_express/bin/python
  2. from config.settings import PORT, BG_DATA_PATH, validate_config, DEVELOPMENT
  3. import os
  4. import uvicorn
  5. from app import create_app, setup_routes
  6. from logging import getLogger
  7. from services.data_service import initialize_db
  8. import redis
  9. logger = getLogger("main")
  10. def main():
  11. """Main application entry point"""
  12. try:
  13. # Validate configuration
  14. if not validate_config():
  15. logger.critical("FATAL: Configuration validation failed.")
  16. logger.error("Please create a .env file with OPENAI_API_KEY='your_key_here'")
  17. with open(".env", "w") as f:
  18. f.write("OPENAI_API_KEY='your_key_here'")
  19. return None
  20. logger.info("Creating FastAPI application")
  21. app = create_app()
  22. logger.info("Setting up application routes")
  23. setup_routes(app)
  24. # Initialize database
  25. logger.info("Initializing database")
  26. redis_client = redis.Redis(host='localhost', port=6379, db=1 if DEVELOPMENT else 0)
  27. # eliminar la lista de usuarios conectados al iniciar
  28. redis_client.delete("connected_users")
  29. initialize_db()
  30. # Display startup information
  31. logger.info(f"Server starting on http://localhost:{PORT}")
  32. if not os.path.exists(BG_DATA_PATH):
  33. logger.warning(f"WARNING: {BG_DATA_PATH} not found. AI assistant will not have specific menu data.")
  34. else:
  35. logger.info(f"AI assistant data loaded from: {os.path.abspath(BG_DATA_PATH)}")
  36. logger.info("Pedidos Express application ready to serve requests")
  37. return app
  38. except Exception as e:
  39. error_msg = f"Critical error during application startup: - {e}"
  40. logger.critical(error_msg)
  41. raise e
  42. app = main()
  43. if not app:
  44. logger.critical("FATAL: Failed to create FastAPI app.")
  45. exit(1)
  46. logger.info("Application initialized successfully")
  47. if __name__ == "__main__":
  48. logger.info(f"Starting server with uvicorn on port {PORT}")
  49. uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info", access_log=False)