Checklist / start_server.py
Ali2206's picture
Add missing files: start_server.py, mongodb.env.example, and fix Dockerfile paths
c0559a6
#!/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()