File size: 2,849 Bytes
c0559a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#!/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()
|