X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/654b652530bb7274ec5dc033375e175f81cb42b1..e1af6a22d357de51c86ac73582beba2b4419227b:/nominatim/server/sanic/server.py diff --git a/nominatim/server/sanic/server.py b/nominatim/server/sanic/server.py index 57b374d0..15887eef 100644 --- a/nominatim/server/sanic/server.py +++ b/nominatim/server/sanic/server.py @@ -16,6 +16,7 @@ from sanic.response import text as TextResponse from nominatim.api import NominatimAPIAsync import nominatim.api.v1 as api_impl +from nominatim.config import Configuration class ParamWrapper(api_impl.ASGIAdaptor): """ Adaptor class for server glue to Sanic framework. @@ -33,13 +34,18 @@ class ParamWrapper(api_impl.ASGIAdaptor): return cast(Optional[str], self.request.headers.get(name, default)) - def error(self, msg: str) -> SanicException: - return SanicException(msg, status_code=400) + def error(self, msg: str, status: int = 400) -> SanicException: + exception = SanicException(msg, status_code=status) + return exception - def create_response(self, status: int, output: str, - content_type: str) -> HTTPResponse: - return TextResponse(output, status=status, content_type=content_type) + + def create_response(self, status: int, output: str) -> HTTPResponse: + return TextResponse(output, status=status, content_type=self.content_type) + + + def config(self) -> Configuration: + return cast(Configuration, self.request.app.ctx.api.config) def _wrap_endpoint(func: api_impl.EndpointFunc)\ @@ -58,7 +64,15 @@ 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) + + legacy_urls = app.ctx.api.config.get_bool('SERVE_LEGACY_URLS') for name, func in api_impl.ROUTES: - app.add_route(_wrap_endpoint(func), f"/{name}", name=f"v1_{name}_simple") + endpoint = _wrap_endpoint(func) + app.add_route(endpoint, f"/{name}", name=f"v1_{name}_simple") + if legacy_urls: + app.add_route(endpoint, f"/{name}.php", name=f"v1_{name}_legacy") return app