62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
# run_app.py
|
|
"""
|
|
Development server that mimics Docker production conditions.
|
|
Uses Gunicorn with Uvicorn workers (like Docker) but with auto-reload enabled.
|
|
Loads environment variables from .env file.
|
|
"""
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Load environment variables from .env file (mimics docker-compose env_file)
|
|
try:
|
|
from dotenv import load_dotenv
|
|
env_path = Path(__file__).parent / '.env'
|
|
if env_path.exists():
|
|
load_dotenv(env_path)
|
|
print(f"✓ Loaded environment from {env_path}")
|
|
else:
|
|
print(f"⚠ No .env file found at {env_path}")
|
|
except ImportError:
|
|
print("⚠ python-dotenv not installed. Run: pip install python-dotenv")
|
|
print(" Environment variables from .env will not be loaded")
|
|
|
|
# Mimic Docker environment variables
|
|
os.environ.setdefault('PYTHONDONTWRITEBYTECODE', '1')
|
|
os.environ.setdefault('PYTHONUNBUFFERED', '1')
|
|
|
|
# Ensure upload folder exists (mimics Dockerfile RUN mkdir)
|
|
UPLOAD_FOLDER = "data/uploads"
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
|
if __name__ == "__main__":
|
|
# Check if we should use Gunicorn (production-like) or Uvicorn (faster dev)
|
|
use_gunicorn = "--gunicorn" in sys.argv
|
|
|
|
if use_gunicorn:
|
|
# Production-like: Gunicorn with Uvicorn workers (mimics Docker CMD exactly)
|
|
import subprocess
|
|
print("🚀 Starting with Gunicorn + Uvicorn workers (Docker production mode)")
|
|
print(" Workers: 4, Host: 0.0.0.0:8000, Timeout: 120s")
|
|
print(" Note: Auto-reload NOT available in Gunicorn mode")
|
|
subprocess.run([
|
|
"gunicorn", "main:app",
|
|
"-k", "uvicorn.workers.UvicornWorker",
|
|
"--bind", "0.0.0.0:8000",
|
|
"--workers", "4",
|
|
"--timeout", "120",
|
|
"--reload" # Gunicorn's reload (watches Python files)
|
|
])
|
|
else:
|
|
# Development: Single Uvicorn worker with fast auto-reload
|
|
import uvicorn
|
|
print("🔧 Starting with Uvicorn (development mode with auto-reload)")
|
|
print(" Host: 0.0.0.0:8000")
|
|
print(" Tip: Use --gunicorn flag to test with production server")
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=True,
|
|
log_level="info"
|
|
)
|