From 5a8d2b99b477e9063ff16b612e257e1b59456a91 Mon Sep 17 00:00:00 2001 From: Carlos Cruz Date: Tue, 21 Oct 2025 09:41:05 +0100 Subject: [PATCH] docker config --- Dockerfile | 15 ++++++++---- app/config.py | 9 ++++++- dev.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 10 ++++---- ui/.env.development | 2 ++ ui/.env.production | 1 + ui/index.html | 25 +++++++++++--------- ui/src/lib/api.ts | 12 +++++++++- 8 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 dev.sh create mode 100644 ui/.env.development create mode 100644 ui/.env.production diff --git a/Dockerfile b/Dockerfile index 8253cef..92a16b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,12 +27,19 @@ COPY app/ ./app/ # Copy built frontend from GitHub Actions build COPY ui/dist ./ui/dist -# Expose port -EXPOSE 8000 +# Create startup script that properly handles runtime PORT variable +RUN echo '#!/bin/sh\n\ +PORT="${PORT:-8080}"\n\ +echo "Starting server on port $PORT"\n\ +exec /app/.venv/bin/uvicorn app.main:app --host 0.0.0.0 --port "$PORT"' > /app/start.sh && \ + chmod +x /app/start.sh + +# Expose port 8080 (Cloud Run default) +EXPOSE 8080 # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ - CMD curl -f http://localhost:8000/ || exit 1 + CMD curl -f http://localhost:${PORT:-8080}/ || exit 1 # Run the application -CMD ["/app/.venv/bin/uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] +CMD ["/app/start.sh"] diff --git a/app/config.py b/app/config.py index 583ab31..11b9d53 100644 --- a/app/config.py +++ b/app/config.py @@ -15,15 +15,22 @@ class DevelopmentConfig(BaseConfig): "http://127.0.0.1:5173", # Vite dev server alternative "http://localhost:4173", # Vite preview server "http://127.0.0.1:4173", # Vite preview server alternative + "http://localhost:8080", # Docker local + "http://localhost:8000", # Alternative local ] class ProductionConfig(BaseConfig): DEBUG = False - CORS_ORIGINS = [""] + # For Cloud Run, you need to add your actual domain + CORS_ORIGINS = os.getenv("CORS_ORIGINS", "").split(",") if os.getenv("CORS_ORIGINS") else ["*"] def get_config(): + # Check for DEBUG env var first, then APP_ENV + if os.getenv("DEBUG", "").lower() == "true": + return DevelopmentConfig + env = os.getenv("APP_ENV", "development") if env == "production": return ProductionConfig diff --git a/dev.sh b/dev.sh new file mode 100644 index 0000000..241c68c --- /dev/null +++ b/dev.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Local development script for pathways-api + +echo "🚀 Starting Pathways API local development environment..." + +# Function to cleanup on exit +cleanup() { + echo "🛑 Stopping services..." + docker-compose down + exit +} + +# Set trap to cleanup on script exit +trap cleanup EXIT INT TERM + +# Check if running in Docker mode or local mode +if [ "$1" = "docker" ]; then + echo "📦 Running in Docker mode..." + + # Build and run with docker-compose + docker-compose build + docker-compose up -d + + echo "✅ API running at http://localhost:8080" + echo "📝 Logs: docker-compose logs -f pathways-api" + + # Wait for services + docker-compose logs -f + +elif [ "$1" = "local" ]; then + echo "💻 Running in local development mode..." + + # Start backend + echo "🔧 Starting backend..." + cd /Users/carlos_cruz/projects/ot/pathways-api + PORT=8080 uvicorn app.main:app --reload --host 0.0.0.0 --port 8080 & + BACKEND_PID=$! + + # Start frontend + echo "🎨 Starting frontend..." + cd ui + npm run dev & + FRONTEND_PID=$! + + echo "✅ Backend running at http://localhost:8080" + echo "✅ Frontend running at http://localhost:5173" + + # Wait for both processes + wait $BACKEND_PID $FRONTEND_PID + +else + echo "Usage: $0 [docker|local]" + echo " docker - Run everything in Docker" + echo " local - Run backend and frontend locally" + exit 1 +fi diff --git a/docker-compose.yml b/docker-compose.yml index f1ab0e1..7d6547c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,16 +6,18 @@ services: context: . dockerfile: Dockerfile ports: - - "8000:8000" + - "8080:8080" environment: - - DEBUG=false - - CORS_ORIGINS=http://localhost:3000,http://localhost:8000 + - PORT=8080 + - DEBUG=true + - APP_ENV=development + - CORS_ORIGINS=http://localhost:3000,http://localhost:5173,http://127.0.0.1:5173,http://localhost:8080 volumes: # Mount data directory for development - ./app/data:/app/app/data:ro restart: unless-stopped healthcheck: - test: [ "CMD", "curl", "-f", "http://localhost:8000/" ] + test: [ "CMD", "curl", "-f", "http://localhost:8080/" ] interval: 30s timeout: 10s retries: 3 diff --git a/ui/.env.development b/ui/.env.development new file mode 100644 index 0000000..5a8eb67 --- /dev/null +++ b/ui/.env.development @@ -0,0 +1,2 @@ +# Add a .env file for local development +VITE_API_URL=http://localhost:8080 diff --git a/ui/.env.production b/ui/.env.production new file mode 100644 index 0000000..460e575 --- /dev/null +++ b/ui/.env.production @@ -0,0 +1 @@ +VITE_API_URL=http://localhost:8080 diff --git a/ui/index.html b/ui/index.html index e4b78ea..53b3037 100644 --- a/ui/index.html +++ b/ui/index.html @@ -1,13 +1,16 @@ - - - - - Vite + React + TS - - -
- - - + + + + + + Vite + React + TS + + + +
+ + + + \ No newline at end of file diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index 8422402..179843d 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -1,4 +1,14 @@ -const API_BASE_URL = 'http://localhost:8000'; +const getApiBaseUrl = () => { + // For production (when served from the same domain) + if (window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1') { + return ''; // Use relative URLs in production + } + + // For local development + return import.meta.env.VITE_API_URL || 'http://localhost:8080'; +}; + +const API_BASE_URL = getApiBaseUrl(); export type Pathway = Record;