From c32cff03889705b3959b510964a143a376385dea Mon Sep 17 00:00:00 2001 From: Paulo Reyes Date: Wed, 28 Jan 2026 03:41:57 +0800 Subject: [PATCH] feat: Initialize Storyline AI Gateway project with a local run script and Docker configuration files. --- .dockerignore | 9 ++++++++ Dockerfile | 29 ++++++++++++++++++++++++++ docker-compose.yml | 13 ++++++++++++ run.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 run.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d313f1d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +venv/ +__pycache__/ +*.pyc +.git/ +.env +.ipynb_checkpoints/ +storyline.db +*.log +docker-compose.override.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..43bb763 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Use Python 3.12 slim image for a lightweight container +FROM python:3.12-slim + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 +ENV PORT=8000 + +# Set working directory +WORKDIR /app + +# Install system dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + libpq-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install Python dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application +COPY . . + +# Expose the port the app runs on +EXPOSE 8000 + +# Command to run the application using uvicorn +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..faff34f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +services: + api: + build: . + container_name: storyline-ai-gateway + ports: + - "8000:8000" + env_file: + - .env + restart: always + volumes: + - .:/app + # Override command for development/auto-reload if needed + command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload diff --git a/run.py b/run.py new file mode 100644 index 0000000..1db7b9e --- /dev/null +++ b/run.py @@ -0,0 +1,52 @@ +import uvicorn +import os +import sys +import subprocess +from dotenv import load_dotenv + +def setup_environment(): + """Ensure dependencies are installed and .env exists.""" + print("šŸš€ Initializing Storyline AI Gateway...") + + # 1. Check for .env file + if not os.path.exists(".env"): + print("āš ļø Warning: .env file not found!") + print("Please create a .env file based on the documentation.") + + # 2. Check for virtual environment and install dependencies if requested + if len(sys.argv) > 1 and sys.argv[1] == "--install": + print("šŸ“¦ Installing dependencies from requirements.txt...") + try: + subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"]) + print("āœ… Dependencies installed successfully.") + except Exception as e: + print(f"āŒ Error installing dependencies: {e}") + sys.exit(1) + +def run_server(): + """Start the FastAPI server.""" + load_dotenv() + + # Configuration + host = os.getenv("HOST", "0.0.0.0") + port = int(os.getenv("PORT", 8000)) + # On a server, we usually don't want reload=True by default for performance + # But for a 'test server', it might be useful. + reload = os.getenv("DEBUG", "false").lower() == "true" + + print(f"\n🌐 Gateway starting at http://{host}:{port}") + print(f"šŸ› ļø Admin Dashboard: http://{host}:{port}/admin") + print(f"šŸ“š API Docs: http://{host}:{port}/docs") + print(f"šŸ”„ Mode: {'Development (Reload On)' if reload else 'Production (Reload Off)'}\n") + + uvicorn.run( + "app.main:app", + host=host, + port=port, + reload=reload, + workers=1 if reload else 4 # Multi-worker for production + ) + +if __name__ == "__main__": + setup_environment() + run_server()