mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-11 05:14:07 +00:00
add support for CORS headers
Adds the additional dependency to sanic-cors for the Sanic server.
This commit is contained in:
@@ -65,7 +65,7 @@ def get_application(project_dir: Path,
|
||||
"""
|
||||
api = NominatimAPIAsync(project_dir, environ)
|
||||
|
||||
app = App()
|
||||
app = App(cors_enable=api.config.get_bool('CORS_NOACCESSCONTROL'))
|
||||
for name, func in api_impl.ROUTES:
|
||||
app.add_route('/' + name, EndpointWrapper(func, api))
|
||||
|
||||
|
||||
@@ -58,6 +58,10 @@ def get_application(project_dir: Path,
|
||||
|
||||
app.ctx.api = NominatimAPIAsync(project_dir, environ)
|
||||
|
||||
if app.ctx.api.config.get_bool('CORS_NOACCESSCONTROL'):
|
||||
from sanic_cors import CORS # pylint: disable=import-outside-toplevel
|
||||
CORS(app)
|
||||
|
||||
for name, func in api_impl.ROUTES:
|
||||
app.add_route(_wrap_endpoint(func), f"/{name}", name=f"v1_{name}_simple")
|
||||
|
||||
|
||||
@@ -15,7 +15,10 @@ from starlette.routing import Route
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import Response
|
||||
from starlette.requests import Request
|
||||
from starlette.middleware import Middleware
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
from nominatim.config import Configuration
|
||||
from nominatim.api import NominatimAPIAsync
|
||||
import nominatim.api.v1 as api_impl
|
||||
|
||||
@@ -55,11 +58,17 @@ def get_application(project_dir: Path,
|
||||
environ: Optional[Mapping[str, str]] = None) -> Starlette:
|
||||
""" Create a Nominatim falcon ASGI application.
|
||||
"""
|
||||
config = Configuration(project_dir, environ)
|
||||
|
||||
routes = []
|
||||
for name, func in api_impl.ROUTES:
|
||||
routes.append(Route(f"/{name}", endpoint=_wrap_endpoint(func)))
|
||||
|
||||
app = Starlette(debug=True, routes=routes)
|
||||
middleware = []
|
||||
if config.get_bool('CORS_NOACCESSCONTROL'):
|
||||
middleware.append(Middleware(CORSMiddleware, allow_origins=['*']))
|
||||
|
||||
app = Starlette(debug=True, routes=routes, middleware=middleware)
|
||||
|
||||
app.state.API = NominatimAPIAsync(project_dir, environ)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user