]> git.openstreetmap.org Git - nominatim.git/commitdiff
add support for CORS headers
authorSarah Hoffmann <lonvia@denofr.de>
Tue, 24 Jan 2023 20:26:32 +0000 (21:26 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Tue, 24 Jan 2023 20:39:19 +0000 (21:39 +0100)
Adds the additional dependency to sanic-cors for the Sanic server.

.github/workflows/ci-tests.yml
.mypy.ini
docs/admin/Installation.md
nominatim/cli.py
nominatim/server/falcon/server.py
nominatim/server/sanic/server.py
nominatim/server/starlette/server.py

index 0f4aea263a7acd510a98c9c53bad47eb14e4c306..a4de7149c059d5882a063822b99ac976076f3b52 100644 (file)
@@ -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
index ef2057d4313cd9d3c2316253b278c97712d1da49..611c3c5d9188c875df5a8243310baa039a5da2f9 100644 (file)
--- a/.mypy.ini
+++ b/.mypy.ini
@@ -1,6 +1,9 @@
 [mypy]
 plugins = sqlalchemy.ext.mypy.plugin
 
+[mypy-sanic_cors.*]
+ignore_missing_imports = True
+
 [mypy-icu.*]
 ignore_missing_imports = True
 
index f6692f5876143d26f3a622ea99dcf7dd4a1b8c94..663d5c379368cc51713d5b5bd5b2b4ec736527de 100644 (file)
@@ -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)
 
index cedbdb4a5f984b0dc9b2bd5e60e450fad2adebd0..d34ef118ed9a48609c455616207d36387e006802 100644 (file)
@@ -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
 
index ddbd2ca64f1b2fcaf2c6b53411f4320bc2cefd81..4295b3f3082d8bee3c6e91b7792bca77e9c9482d 100644 (file)
@@ -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))
 
index 57b374d0742de0041253b0c2e947b31cdf5e6a42..98e6b6e28b48a817a7e5bd061cf6358b2326ac94 100644 (file)
@@ -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")
 
index 2bf569edd2b9c21ba778b8aa3de3a62d2fefe85e..dfbdc50290eae1230f36b5ba6b35ad8de6b256e1 100644 (file)
@@ -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)