1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Server implementation using the falcon webserver framework.
10 from pathlib import Path
14 from nominatim.api import NominatimAPIAsync
15 from nominatim.apicmd.status import StatusResult
16 import nominatim.result_formatter.v1 as formatting
19 'text': falcon.MEDIA_TEXT,
20 'xml': falcon.MEDIA_XML
25 def __init__(self, project_dir: Path):
26 self.api = NominatimAPIAsync(project_dir)
29 for rtype in (StatusResult, ):
30 self.formatters[rtype] = formatting.create(rtype)
32 def parse_format(self, req, rtype, default):
33 req.context.format = req.get_param('format', default=default)
34 req.context.formatter = self.formatters[rtype]
36 if not req.context.formatter.supports_format(req.context.format):
37 raise falcon.HTTPBadRequest(
38 description="Parameter 'format' must be one of: " +
39 ', '.join(req.context.formatter.list_formats()))
42 def format_response(self, req, resp, result):
43 resp.text = req.context.formatter.format(result, req.context.format)
44 resp.content_type = CONTENT_TYPE.get(req.context.format, falcon.MEDIA_JSON)
47 async def on_get_status(self, req, resp):
48 self.parse_format(req, StatusResult, 'text')
50 result = await self.api.status()
52 self.format_response(req, resp, result)
55 def get_application(project_dir: Path) -> falcon.asgi.App:
56 app = falcon.asgi.App()
58 api = NominatimV1(project_dir)
60 app.add_route('/status', api, suffix='status')