| |
| """ |
| 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""" |
| |
| |
| server_dir = Path(__file__).parent |
| sys.path.insert(0, str(server_dir)) |
| |
| |
| 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() |
| |
| |
| 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) |
| |
| |
| from dotenv import load_dotenv |
| load_dotenv(env_file) |
| |
| |
| 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: |
| |
| 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() |
|
|