Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add cors middleware to Nutripatrol API #39

Merged
merged 8 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ POSTGRES_PASSWORD=postgres
OFF_TLD=net

# Environment name (mostly used for Sentry): dev, staging, prod
ENVIRONMENT=dev
ENVIRONMENT=dev

# The URL of local nutripatrol frontend
CORS_ALLOW_ORIGINS=["http://localhost:5173"]
1 change: 1 addition & 0 deletions .github/workflows/container-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ x-api-common: &api-common
- POSTGRES_PASSWORD
- POSTGRES_DB
- POSTGRES_HOST
- CORS_ALLOW_ORIGINS
networks:
- default

Expand Down
10 changes: 5 additions & 5 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}