From 6e40b87a56d26880235e0f4282a4edc526840590 Mon Sep 17 00:00:00 2001 From: Matt Fisher Date: Tue, 16 Jul 2024 10:18:11 -0600 Subject: [PATCH] Add CORS middleware to support requests from other origins Rename existing (incorrectly-named) middleware module to make space! --- src/aross_stations_db/api/__init__.py | 8 ++++++++ .../{middleware.py => api/dependencies.py} | 0 src/aross_stations_db/api/v1/climatology.py | 2 +- src/aross_stations_db/api/v1/stations.py | 2 +- src/aross_stations_db/api/v1/timeseries.py | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) rename src/aross_stations_db/{middleware.py => api/dependencies.py} (100%) diff --git a/src/aross_stations_db/api/__init__.py b/src/aross_stations_db/api/__init__.py index f3d0e7a..1d06b7e 100644 --- a/src/aross_stations_db/api/__init__.py +++ b/src/aross_stations_db/api/__init__.py @@ -1,4 +1,5 @@ from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware from aross_stations_db.api.v1.routes import router as v1_router @@ -9,6 +10,13 @@ ), ) api.include_router(v1_router, prefix="/v1", tags=["v1"]) +api.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) @api.get("/") diff --git a/src/aross_stations_db/middleware.py b/src/aross_stations_db/api/dependencies.py similarity index 100% rename from src/aross_stations_db/middleware.py rename to src/aross_stations_db/api/dependencies.py diff --git a/src/aross_stations_db/api/v1/climatology.py b/src/aross_stations_db/api/v1/climatology.py index b5f70b2..3a10f50 100644 --- a/src/aross_stations_db/api/v1/climatology.py +++ b/src/aross_stations_db/api/v1/climatology.py @@ -5,11 +5,11 @@ from geoalchemy2 import WKTElement from sqlalchemy.orm import Session +from aross_stations_db.api.dependencies import get_db_session from aross_stations_db.api.v1.output import ( ClimatologyJsonElement, climatology_query_results_to_json, ) -from aross_stations_db.middleware import get_db_session from aross_stations_db.query import climatology_query router = APIRouter() diff --git a/src/aross_stations_db/api/v1/stations.py b/src/aross_stations_db/api/v1/stations.py index 3aebaf1..47ac6c1 100644 --- a/src/aross_stations_db/api/v1/stations.py +++ b/src/aross_stations_db/api/v1/stations.py @@ -5,11 +5,11 @@ from geoalchemy2 import WKTElement from sqlalchemy.orm import Session +from aross_stations_db.api.dependencies import get_db_session from aross_stations_db.api.v1.output import ( StationsGeoJson, stations_query_results_to_geojson, ) -from aross_stations_db.middleware import get_db_session from aross_stations_db.query import stations_query router = APIRouter() diff --git a/src/aross_stations_db/api/v1/timeseries.py b/src/aross_stations_db/api/v1/timeseries.py index 84ce26b..30cdc48 100644 --- a/src/aross_stations_db/api/v1/timeseries.py +++ b/src/aross_stations_db/api/v1/timeseries.py @@ -5,11 +5,11 @@ from geoalchemy2 import WKTElement from sqlalchemy.orm import Session +from aross_stations_db.api.dependencies import get_db_session from aross_stations_db.api.v1.output import ( TimeseriesJsonElement, timeseries_query_results_to_json, ) -from aross_stations_db.middleware import get_db_session from aross_stations_db.query import timeseries_query router = APIRouter()