main.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. redis_client.delete("chat_history")
  30. initialize_db()
  31. # Display startup information
  32. logger.info(f"Server starting on http://localhost:{PORT}")
  33. if not os.path.exists(BG_DATA_PATH):
  34. logger.warning(f"WARNING: {BG_DATA_PATH} not found. AI assistant will not have specific menu data.")
  35. else:
  36. logger.info(f"AI assistant data loaded from: {os.path.abspath(BG_DATA_PATH)}")
  37. logger.info("Pedidos Express application ready to serve requests")
  38. return app
  39. except Exception as e:
  40. error_msg = f"Critical error during application startup: - {e}"
  41. logger.critical(error_msg)
  42. raise e
  43. app = main()
  44. if not app:
  45. logger.critical("FATAL: Failed to create FastAPI app.")
  46. exit(1)
  47. logger.info("Application initialized successfully")
  48. if __name__ == "__main__":
  49. logger.info(f"Starting server with uvicorn on port {PORT}")
  50. uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info", access_log=False)