Skip to content

Commit

Permalink
feat: origin configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed Dec 7, 2023
1 parent 298ef85 commit c5f3d5c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion zonalda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,24 @@ async def geoloc(

app = FastAPI()
app.mount("/api", api)
middleware_args: dict[str, str | list[str]]
if os.getenv("DEVELOPMENT", False):
LOGGER.info(
"Running in development mode, will allow requests from http://localhost:*"
)
# Allow requests from localhost dev servers
middleware_args = dict(
allow_origin_regex="http://localhost(:.*)?",
)
else:
# Allow requests *only* from ZONALDA app (or otherwise configured site name)
middleware_args = dict(
allow_origins=[
os.getenv("ORIGIN", "https://dhdaines.github.io/zonalda"),
],
)
app.add_middleware(
CORSMiddleware,
allow_methods=["GET", "OPTIONS"],
allow_origin_regex="(http://localhost(:.*)?|https://zonalda.ecolingui.ca)",
**middleware_args
)

0 comments on commit c5f3d5c

Please sign in to comment.