Skip to content

Commit

Permalink
Add CORS headers to allow API calls from other websites
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Dec 8, 2023
1 parent 0a74837 commit 9b30215
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from urllib.parse import urlparse

from aiohttp import web
from aiohttp_cors import ResourceOptions, setup as cors_setup
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration

Expand Down Expand Up @@ -78,12 +79,26 @@ def run_server(
sentry_sdk.set_tag(
"backend_url", backend_url
) # Tag all requests for the lifecycle of the app with the overpass URL used

app = web.Application()
app["backend_client"] =backend_client(
backend_url, user_agent, cache_dir, cache_days, cache_size)
app["backend_client"] = backend_client(
backend_url, user_agent, cache_dir, cache_days, cache_size
)
app.add_routes(
[
web.get(r"/tiles/{zoom:\d+}/{x:\d+}/{y:\d+}.json", tile_handler),
]
)

# Allow web apps running on any domain to make API calls to this server
cors = cors_setup(app, defaults={
"*": ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
)
})
for route in list(app.router.routes()):
cors.add(route)

web.run_app(app, port=port)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
aiohttp==3.8.4
aiohttp_cors==0.7.0
aiopg==1.4.0
osm2geojson==0.2.3
shapely==2.0.1
Expand Down

0 comments on commit 9b30215

Please sign in to comment.