From 97b3ea52bc75bff9511588cbc4d1719a90132fd9 Mon Sep 17 00:00:00 2001 From: AlexeerCT Date: Wed, 31 Dec 2025 10:37:23 -0600 Subject: [PATCH] feat: Update environment variables and Docker configurations for production setup --- .env.example | 1 + README.md | 8 +++++++- backend/requirements.txt | 1 + docker-compose.prod.yml | 37 +++++++++++++++++++--------------- frontend/Dockerfile.prod | 12 ++++++++++- frontend/package.json | 1 + frontend/src/lib/server/api.ts | 9 ++++++++- frontend/svelte.config.js | 7 ++++++- 8 files changed, 56 insertions(+), 20 deletions(-) diff --git a/.env.example b/.env.example index b303d5e4..c66ef877 100644 --- a/.env.example +++ b/.env.example @@ -32,6 +32,7 @@ CORE_DB_PASSWORD=postgres # ----- Frontend ----- NODE_ENV=development VITE_API_URL=http://localhost:8000/api +INTERNAL_API_URL=http://backend:8000/api VITE_KEYCLOAK_REALM=master VITE_KEYCLOAK_URL=http://localhost:8080 VITE_KEYCLOAK_CLIENT_ID=anexo76-frontend diff --git a/README.md b/README.md index 956de442..d6d16209 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,13 @@ python -c "from core.database import init_db; init_db()" ``` docker build -t dev.aduanasoft.com/anexo76/backend:latest -f ./backend/Dockerfile ./backend -docker build -t dev.aduanasoft.com/anexo76/frontend:latest -f ./frontend/Dockerfile.prod ./frontend +docker build \ +--build-arg VITE_API_URL=https://anexo76-dev.aduanasoft.com/api/ \ +--build-arg VITE_KEYCLOAK_URL=https://anexo76-dev.aduanasoft.com/kcauth/ \ +--build-arg INTERNAL_API_URL=http://backend:3467/api/ \ +-t dev.aduanasoft.com/anexo76/frontend:latest \ +-f ./frontend/Dockerfile.prod \ +./frontend ``` Publica en el registro (ajusta el registry si corresponde): diff --git a/backend/requirements.txt b/backend/requirements.txt index 9f13d289..1e600ac7 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,6 +1,7 @@ # Core Framework fastapi==0.119.0 uvicorn[standard]==0.37.0 +gunicorn==23.0.0 pydantic==2.12.3 pydantic[email]==2.12.3 pydantic-settings==2.11.0 diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 4339e0a8..7a676940 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -87,18 +87,19 @@ services: KC_DB_USERNAME: postgres KC_DB_PASSWORD: ${POSTGRES_KEYCLOAK_PASSWORD:-postgres} KC_DB_SCHEMA: public - KC_HOSTNAME: localhost + KC_HOSTNAME: anexo76-dev.aduanasoft.com KC_HTTP_ENABLED: "true" KC_HOSTNAME_STRICT: "false" KC_HOSTNAME_STRICT_HTTPS: "false" + KC_PROXY: edge KC_PROXY_HEADERS: "xforwarded" KC_HEALTH_ENABLED: "true" KC_METRICS_ENABLED: "true" KC_HOSTNAME_PATH: /kcauth - KC_LOG_LEVEL: INFO + KC_LOG_LEVEL: INFO JAVA_OPTS_APPEND: "-Xms256m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true" command: - - start-dev + - start - --db=postgres - --db-url-host=postgres-keycloak - --http-relative-path=/kcauth @@ -165,16 +166,26 @@ services: condition: service_healthy keycloak: condition: service_healthy - volumes: - - ./backend:/app - - backend_cache:/app/__pycache__ + volumes: - ./scripts/backend-entrypoint.sh:/entrypoint.sh:ro networks: - backend-net - frontend-net restart: unless-stopped entrypoint: ["/entrypoint.sh"] - command: ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--log-level", "info"] + command: + [ + "gunicorn", + "main:app", + "-k", + "uvicorn.workers.UvicornWorker", + "-w", + "${WEB_CONCURRENCY:-4}", + "-b", + "0.0.0.0:8000", + "--log-level", + "info" + ] healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:8000/api/health || exit 1"] interval: 15s @@ -195,13 +206,7 @@ services: # Frontend - SvelteKit frontend: - build: - context: ./frontend - dockerfile: Dockerfile.prod - args: - - BUILDKIT_INLINE_CACHE=1 - - VITE_API_URL=https://anexo76-dev.aduanasoft.com/api/ - - VITE_KEYCLOAK_URL=https://anexo76-dev.aduanasoft.com/kcauth/ + image: dev.aduanasoft.com/anexo76/frontend:latest container_name: anexo76-frontend environment: - NODE_ENV=${NODE_ENV:-development} @@ -213,7 +218,7 @@ services: - KEYCLOAK_URL=${KEYCLOAK_URL:-http://keycloak:8080/kcauth} - KEYCLOAK_REALM=${KEYCLOAK_REALM:-master} - KEYCLOAK_CLIENT_ID=${KEYCLOAK_CLIENT_ID:-anexo76-backend} - - KEYCLOAK_CLIENT_SECRET=${KEYCLOAK_CLIENT_SECRET:-zRU5NuvUFtBSOuh7Kdc372AItoWGLgz9} + - KEYCLOAK_CLIENT_SECRET=${KEYCLOAK_CLIENT_SECRET:-dev-secret} ports: - "5111:5173" depends_on: @@ -226,7 +231,7 @@ services: - frontend-net - auth-net restart: unless-stopped - command: ["node", "build"] + command: ["pnpm", "start"] healthcheck: test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:5173/ || exit 1"] interval: 15s diff --git a/frontend/Dockerfile.prod b/frontend/Dockerfile.prod index 754f6fed..5d7ea7a6 100644 --- a/frontend/Dockerfile.prod +++ b/frontend/Dockerfile.prod @@ -19,6 +19,12 @@ RUN pnpm install --frozen-lockfile ARG VITE_API_URL ENV VITE_API_URL=${VITE_API_URL} +ARG VITE_KEYCLOAK_URL +ENV VITE_KEYCLOAK_URL=${VITE_KEYCLOAK_URL} + +ARG INTERNAL_API_URL +ENV INTERNAL_API_URL=${INTERNAL_API_URL} + # Copiar el resto del código COPY . . @@ -55,5 +61,9 @@ ENV NODE_ENV=production ENV PORT=5173 ENV HOST=0.0.0.0 +# IMPORTANTE: Estas variables se pueden sobrescribir en docker-compose +# pero necesitamos valores por defecto para el build +ENV INTERNAL_API_URL=http://backend:8000/api/ + # Ejecutar aplicación con Node.js -CMD ["node", "build"] +CMD ["pnpm", "start"] diff --git a/frontend/package.json b/frontend/package.json index 8deac80f..ca906270 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -8,6 +8,7 @@ "i18n:compile": "paraglide-js compile --project ./project.inlang --outdir ./src/lib/paraglide", "build": "vite build", "preview": "vite preview", + "start": "node build/index.js", "prepare": "svelte-kit sync || echo ''", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", diff --git a/frontend/src/lib/server/api.ts b/frontend/src/lib/server/api.ts index 41ec018b..d0d1caeb 100644 --- a/frontend/src/lib/server/api.ts +++ b/frontend/src/lib/server/api.ts @@ -10,12 +10,19 @@ import { redirect, type Cookies } from '@sveltejs/kit'; * Automáticamente reemplaza localhost/127.0.0.1 con 'backend' para Docker */ export function getServerApiUrl(): string { + // Primero intentar con INTERNAL_API_URL (para llamadas server-side en Docker) let apiUrl = process.env.INTERNAL_API_URL; + // Si no está definida, usar VITE_API_URL del entorno runtime (no import.meta.env) + if (!apiUrl) { + apiUrl = process.env.VITE_API_URL; + } + + // Como último recurso, usar el valor de build-time if (!apiUrl) { apiUrl = import.meta.env.VITE_API_URL; // Reemplazar 'localhost' con 'backend' para llamadas desde el servidor (SSR) - apiUrl = apiUrl?.replace('localhost', 'backend').replace('127.0.0.1', 'backend'); + apiUrl = apiUrl?.replace('localhost', 'backend').replace('127.0.0.1', 'backend').replace('anexo76-dev.aduanasoft.com', 'backend'); } // Normalizar la URL: asegurar que termine con '/' diff --git a/frontend/svelte.config.js b/frontend/svelte.config.js index 03c17f28..d3ef3f6a 100644 --- a/frontend/svelte.config.js +++ b/frontend/svelte.config.js @@ -6,7 +6,12 @@ const config = { // Consult https://svelte.dev/docs/kit/integrations // for more information about preprocessors preprocess: vitePreprocess(), - kit: { adapter: adapter() } + kit: { + adapter: adapter(), + csrf: { + checkOrigin: false + } + } }; export default config;