diff --git a/.env b/.env index db3cd67..5c71df2 100644 --- a/.env +++ b/.env @@ -28,4 +28,7 @@ POSTGRES_PASSWORD=postgres OFF_TLD=net # Environment name (mostly used for Sentry): dev, staging, prod -ENVIRONMENT=dev \ No newline at end of file +ENVIRONMENT=dev + +# The URL of local nutripatrol frontend +CORS_ALLOW_ORIGINS=["http://localhost:5173"] diff --git a/.github/workflows/container-deploy.yml b/.github/workflows/container-deploy.yml index ef4126c..6f47d14 100644 --- a/.github/workflows/container-deploy.yml +++ b/.github/workflows/container-deploy.yml @@ -115,6 +115,7 @@ jobs: echo "ENVIRONMENT=${{ env.ENVIRONMENT }}" >> .env # Expose API on port 9010 echo "API_EXPOSE=0.0.0.0:9010" >> .env + echo 'CORS_ALLOW_ORIGINS=["https://nutripatrol.openfoodfacts.net", "https://nutripatrol.openfoodfacts.org"]' >> .env echo "POSTGRES_HOST=postgres" >> .env echo "POSTGRES_DB=postgres" >> .env echo "POSTGRES_USER=postgres" >> .env diff --git a/app/api.py b/app/api.py index eed1323..de6d6a2 100644 --- a/app/api.py +++ b/app/api.py @@ -5,6 +5,7 @@ from typing import Any from fastapi import APIRouter, FastAPI, HTTPException, Request +from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse, PlainTextResponse from fastapi.templating import Jinja2Templates from openfoodfacts import Flavor @@ -68,6 +69,15 @@ "name": " AGPL-3.0", "url": "https://www.gnu.org/licenses/agpl-3.0.en.html", }, + docs_url="/api/docs", + openapi_url="/api/openapi.json", +) +app.add_middleware( + CORSMiddleware, + allow_origins=settings.cors_allow_origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], ) api_v1_router = APIRouter(prefix="/api/v1") templates = Jinja2Templates(directory=Path(__file__).parent / "templates") diff --git a/app/config.py b/app/config.py index 16b90c9..72a6850 100644 --- a/app/config.py +++ b/app/config.py @@ -2,6 +2,7 @@ from pathlib import Path from openfoodfacts import Environment +from pydantic import Field from pydantic_settings import BaseSettings PROJECT_DIR = Path(__file__).parent.parent @@ -38,6 +39,7 @@ class Settings(BaseSettings): postgres_user: str = "postgres" postgres_password: str = "postgres" postgres_port: int = 5432 + cors_allow_origins: list[str] = Field(default_factory=list) off_tld: Environment = Environment.net environment: str = "dev" migration_dir: Path = PROJECT_DIR / "migrations" diff --git a/docker-compose.yml b/docker-compose.yml index e5d0cd5..807df3a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ x-api-common: &api-common - POSTGRES_PASSWORD - POSTGRES_DB - POSTGRES_HOST + - CORS_ALLOW_ORIGINS networks: - default diff --git a/nginx.conf b/nginx.conf index 9a1fef7..78c1f70 100644 --- a/nginx.conf +++ b/nginx.conf @@ -59,10 +59,10 @@ http { expires max; } - # Make vue.js app available under /app - # location /app/ { - # alias /var/static/app/; - # try_files $uri $uri/ /app/index.html; - # } + # Make react.js app available under /app + location /app/ { + alias /var/static/app/; + try_files $uri $uri/ /app/index.html; + } } } \ No newline at end of file