53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to run the FastAPI server for DICOM migration service.
|
|
"""
|
|
import os
|
|
import sys
|
|
import signal
|
|
import uvicorn
|
|
import argparse
|
|
|
|
def parse_args():
|
|
"""Parse command-line arguments."""
|
|
parser = argparse.ArgumentParser(description='DICOM Migration API Server')
|
|
parser.add_argument('--host', default='0.0.0.0', help='Host to bind to')
|
|
parser.add_argument('--port', type=int, default=8000, help='Port to bind to')
|
|
parser.add_argument('--token', help='API token for authentication (overrides environment variable)')
|
|
parser.add_argument('--reload', action='store_true', help='Enable auto-reload for development')
|
|
return parser.parse_args()
|
|
|
|
def handle_exit(signum, frame):
|
|
"""Handle exit signals gracefully."""
|
|
print(f"Received signal {signum}, shutting down gracefully...")
|
|
# Let uvicorn handle the exit
|
|
sys.exit(0)
|
|
|
|
def main():
|
|
"""Main function to run the API server."""
|
|
args = parse_args()
|
|
|
|
# Set API token if provided
|
|
if args.token:
|
|
os.environ['API_TOKEN'] = args.token
|
|
|
|
# Check if API_TOKEN is set
|
|
if not os.environ.get('API_TOKEN'):
|
|
print("WARNING: API_TOKEN not set. Using default token 'token-his2-untuk-hit-api-migrasi-clarity'.")
|
|
print("Set environment variable API_TOKEN or use --token argument for better security.")
|
|
|
|
# Set up signal handlers for graceful shutdown
|
|
signal.signal(signal.SIGINT, handle_exit)
|
|
signal.signal(signal.SIGTERM, handle_exit)
|
|
|
|
print(f"Starting DICOM Migration API server on {args.host}:{args.port}")
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=args.host,
|
|
port=args.port,
|
|
reload=args.reload
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|