#!/usr/bin/env python3 """ FastAPI Server Startup Script ============================= This script provides an easy way to start the FastAPI server with proper configuration and error handling. Usage: python start_server.py python start_server.py --port 8000 python start_server.py --host 0.0.0.0 --port 5000 """ import argparse import sys import os import uvicorn from pathlib import Path def main(): """Main function to start the FastAPI server""" # Add the server directory to Python path server_dir = Path(__file__).parent sys.path.insert(0, str(server_dir)) # Parse command line arguments parser = argparse.ArgumentParser(description='Start the Audit Checklist FastAPI server') parser.add_argument('--host', default='0.0.0.0', help='Host to bind the server to (default: 0.0.0.0)') parser.add_argument('--port', type=int, default=5000, help='Port to bind the server to (default: 5000)') parser.add_argument('--reload', action='store_true', default=True, help='Enable auto-reload for development') parser.add_argument('--log-level', default='info', choices=['debug', 'info', 'warning', 'error'], help='Log level') args = parser.parse_args() # Check if environment file exists env_file = server_dir / 'mongodb.env' if not env_file.exists(): print("āŒ Error: mongodb.env file not found!") print("Please create the mongodb.env file with your MongoDB connection string.") print("Example:") print("MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database") print("PORT=5000") print("CORS_ORIGIN=http://localhost:3000") sys.exit(1) # Load environment variables from dotenv import load_dotenv load_dotenv(env_file) # Check if MongoDB URI is set if not os.getenv('MONGODB_URI'): print("āŒ Error: MONGODB_URI not found in mongodb.env file!") sys.exit(1) print("šŸš€ Starting Audit Checklist FastAPI Server...") print(f"šŸ“ Host: {args.host}") print(f"šŸ”Œ Port: {args.port}") print(f"šŸ”„ Auto-reload: {'Enabled' if args.reload else 'Disabled'}") print(f"šŸ“Š Log level: {args.log_level}") print(f"šŸ“š API Documentation: http://{args.host}:{args.port}/docs") print(f"šŸ” Health Check: http://{args.host}:{args.port}/health") print("-" * 50) try: # Start the server uvicorn.run( "main:app", host=args.host, port=args.port, reload=args.reload, log_level=args.log_level, access_log=True ) except KeyboardInterrupt: print("\nšŸ‘‹ Server stopped by user") except Exception as e: print(f"āŒ Error starting server: {e}") sys.exit(1) if __name__ == "__main__": main()