Environment methods

This commit is contained in:
2025-08-23 19:01:36 +01:00
parent cba9d1e341
commit 2a85845835
146 changed files with 370 additions and 383 deletions
+91 -29
View File
@@ -1,4 +1,6 @@
import os
import argparse
import sys
from modules.logger_tool import initialise_logger
logger = initialise_logger(__name__, os.getenv("LOG_LEVEL"), os.getenv("LOG_PATH"), 'default', True)
from fastapi import FastAPI, HTTPException
@@ -100,34 +102,94 @@ def initialize_with_retry(max_attempts: int = 3, initial_delay: int = 5) -> bool
return False
if __name__ == "__main__":
import uvicorn
import os
# Run initialization with retry logic
if not initialize_with_retry():
logger.error("Failed to initialize system after multiple attempts")
# Continue anyway to allow the API to start and handle health checks
if os.getenv('BACKEND_DEV_MODE') == 'true':
logger.info("Running with Reload")
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.getenv('PORT_BACKEND', 8000)),
log_level="info",
proxy_headers=True,
timeout_keep_alive=10,
reload=True
)
def run_initialization_mode():
"""Run only the initialization process"""
logger.info("Running in initialization mode")
logger.info("Starting system initialization...")
if initialize_with_retry():
logger.info("Initialization completed successfully")
return True
else:
logger.info("Running without Reload and without SSL (behind reverse proxy)")
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.getenv('PORT_BACKEND', 8000)), # <-- not 443
log_level="info",
proxy_headers=True,
timeout_keep_alive=10,
workers=int(os.getenv('UVICORN_WORKERS', '1'))
logger.error("Initialization failed after multiple attempts")
return False
def run_development_mode():
"""Run the server in development mode with auto-reload"""
logger.info("Running in development mode")
logger.info("Starting uvicorn server with auto-reload...")
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.getenv('UVICORN_PORT', 8000)),
log_level=os.getenv('LOG_LEVEL', 'info'),
proxy_headers=True,
timeout_keep_alive=10,
reload=True
)
def run_production_mode():
"""Run the server in production mode"""
logger.info("Running in production mode")
logger.info("Starting uvicorn server in production mode...")
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.getenv('UVICORN_PORT', 8000)),
log_level=os.getenv('LOG_LEVEL', 'info'),
proxy_headers=True,
timeout_keep_alive=10,
workers=int(os.getenv('UVICORN_WORKERS', '1'))
)
def parse_arguments():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(
description="ClassroomCopilot API Server",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Startup modes:
init - Run initialization scripts (database setup, etc.)
dev - Run development server with auto-reload
prod - Run production server (for Docker/containerized deployment)
"""
)
parser.add_argument(
'--mode', '-m',
choices=['init', 'dev', 'prod'],
default='dev',
help='Startup mode (default: dev)'
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
# Set environment variable for backward compatibility
if args.mode == 'dev':
os.environ['BACKEND_DEV_MODE'] = 'true'
else:
os.environ['BACKEND_DEV_MODE'] = 'false'
logger.info(f"Starting ClassroomCopilot API in {args.mode} mode")
if args.mode == 'init':
# Run initialization only
success = run_initialization_mode()
sys.exit(0 if success else 1)
elif args.mode == 'dev':
# Run development server
run_development_mode()
elif args.mode == 'prod':
# Run production server
run_production_mode()
else:
logger.error(f"Invalid mode: {args.mode}")
sys.exit(1)