main.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import uvicorn
  3. from app import create_app, setup_routes
  4. from config.settings import PORT, OPENAI_API_KEY, BG_DATA_PATH,DEVELOPMENT, validate_config
  5. from logging import getLogger
  6. from threading import Thread
  7. from services.logging_service import structured_logger, LogLevel
  8. from services.data_service import initialize_db
  9. logger = getLogger("main")
  10. def main():
  11. """Main application entry point"""
  12. logger.info("Starting Pedidos Express application")
  13. structured_logger.log_system_event(
  14. "Application startup initiated",
  15. LogLevel.INFO,
  16. {
  17. "port": PORT,
  18. "development_mode": DEVELOPMENT,
  19. "bg_data_path": BG_DATA_PATH
  20. }
  21. )
  22. try:
  23. # Validate configuration
  24. if not validate_config():
  25. logger.critical("FATAL: Configuration validation failed.")
  26. structured_logger.log_system_event(
  27. "Application startup failed: configuration validation error",
  28. LogLevel.CRITICAL,
  29. {"validation_step": "configuration"}
  30. )
  31. if not OPENAI_API_KEY:
  32. logger.error("Please create a .env file with OPENAI_API_KEY='your_key_here'")
  33. with open(".env", "w") as f:
  34. f.write("OPENAI_API_KEY='your_key_here'")
  35. return None
  36. structured_logger.log_system_event(
  37. "Configuration validation successful",
  38. LogLevel.INFO
  39. )
  40. # Create and configure app
  41. logger.info("Creating FastAPI application")
  42. app = create_app()
  43. logger.info("Setting up application routes")
  44. setup_routes(app)
  45. structured_logger.log_system_event(
  46. "FastAPI application created and routes configured",
  47. LogLevel.INFO
  48. )
  49. # Initialize database
  50. logger.info("Initializing database")
  51. initialize_db()
  52. structured_logger.log_system_event(
  53. "Database initialization completed",
  54. LogLevel.INFO
  55. )
  56. # Display startup information
  57. logger.info(f"Server starting on http://localhost:{PORT}")
  58. if not os.path.exists(BG_DATA_PATH):
  59. logger.warning(f"WARNING: {BG_DATA_PATH} not found. AI assistant will not have specific menu data.")
  60. structured_logger.log_system_event(
  61. "AI assistant data file not found",
  62. LogLevel.WARNING,
  63. {
  64. "bg_data_path": BG_DATA_PATH,
  65. "impact": "AI assistant will have limited functionality"
  66. }
  67. )
  68. else:
  69. logger.info(f"AI assistant data loaded from: {os.path.abspath(BG_DATA_PATH)}")
  70. structured_logger.log_system_event(
  71. "AI assistant data loaded successfully",
  72. LogLevel.INFO,
  73. {"bg_data_path": os.path.abspath(BG_DATA_PATH)}
  74. )
  75. structured_logger.log_system_event(
  76. "Application startup completed successfully",
  77. LogLevel.INFO,
  78. {
  79. "server_url": f"http://localhost:{PORT}",
  80. "development_mode": DEVELOPMENT
  81. }
  82. )
  83. logger.info("Pedidos Express application ready to serve requests")
  84. return app
  85. except Exception as e:
  86. error_msg = f"Critical error during application startup: {e}"
  87. logger.critical(error_msg)
  88. structured_logger.log_system_event(
  89. "Application startup failed with critical error",
  90. LogLevel.CRITICAL,
  91. {
  92. "error": str(e),
  93. "error_type": type(e).__name__
  94. }
  95. )
  96. return None
  97. app = main()
  98. if not app:
  99. logger.critical("FATAL: Failed to create FastAPI app.")
  100. structured_logger.log_system_event(
  101. "Application failed to start - FastAPI app creation failed",
  102. LogLevel.CRITICAL
  103. )
  104. exit(1)
  105. logger.info("Application initialized successfully")
  106. if __name__ == "__main__":
  107. logger.info(f"Starting server with uvicorn on port {PORT}")
  108. structured_logger.log_system_event(
  109. "Starting uvicorn server",
  110. LogLevel.INFO,
  111. {
  112. "host": "0.0.0.0",
  113. "port": PORT,
  114. "log_level": "info"
  115. }
  116. )
  117. uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info", access_log=False)