mirror of
https://github.com/osm-search/Nominatim.git
synced 2024-08-17 07:00:34 +03:00
add support for CORS headers
Adds the additional dependency to sanic-cors for the Sanic server.
This commit is contained in:
parent
654b652530
commit
e490a30a4a
2
.github/workflows/ci-tests.yml
vendored
2
.github/workflows/ci-tests.yml
vendored
@ -107,7 +107,7 @@ jobs:
|
||||
if: matrix.flavour == 'oldstuff'
|
||||
|
||||
- name: Install Python webservers
|
||||
run: pip3 install falcon sanic sanic-testing starlette
|
||||
run: pip3 install falcon sanic sanic-testing sanic-cors starlette
|
||||
|
||||
- name: Install latest pylint/mypy
|
||||
run: pip3 install -U pylint mypy types-PyYAML types-jinja2 types-psycopg2 types-psutil types-requests typing-extensions asgi_lifespan sqlalchemy2-stubs
|
||||
|
@ -1,6 +1,9 @@
|
||||
[mypy]
|
||||
plugins = sqlalchemy.ext.mypy.plugin
|
||||
|
||||
[mypy-sanic_cors.*]
|
||||
ignore_missing_imports = True
|
||||
|
||||
[mypy-icu.*]
|
||||
ignore_missing_imports = True
|
||||
|
||||
|
@ -66,7 +66,7 @@ For running the experimental Python frontend:
|
||||
|
||||
* one of the following web frameworks:
|
||||
* [falcon](https://falconframework.org/) (3.0+)
|
||||
* [sanic](https://sanic.dev)
|
||||
* [sanic](https://sanic.dev) and (optionally) [sanic-cors](https://github.com/ashleysommer/sanic-cors)
|
||||
* [starlette](https://www.starlette.io/)
|
||||
* [uvicorn](https://www.uvicorn.org/) (only with falcon and starlette framworks)
|
||||
|
||||
|
@ -236,7 +236,7 @@ class AdminServe:
|
||||
server_module = importlib.import_module('nominatim.server.sanic.server')
|
||||
|
||||
app = server_module.get_application(args.project_dir)
|
||||
app.run(host=host, port=port, debug=True)
|
||||
app.run(host=host, port=port, debug=True, single_process=True)
|
||||
else:
|
||||
import uvicorn # pylint: disable=import-outside-toplevel
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user