From: Sarah Hoffmann Date: Wed, 3 Feb 2021 15:17:46 +0000 (+0100) Subject: replace make serve with nominatim serve command X-Git-Tag: v3.7.0~41^2 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/0b2abfb11570de36fd73a6c43c333aeb78d42a97?hp=b2f8fb620175157d97254a4163f6c5bba62199db replace make serve with nominatim serve command With the website directory now tied to the project directory instead of the build directory, it is no longer possible to use make for running the web server. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index f5820c7f..f7d396d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,22 +117,6 @@ if (BUILD_IMPORTER) ${PROJECT_BINARY_DIR}/nominatim) endif() -#----------------------------------------------------------------------------- -# Targets for running a development webserver from the build directory. -#----------------------------------------------------------------------------- - -if (BUILD_API) - add_custom_target(serve - php -S 127.0.0.1:8088 - WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/website - ) - - add_custom_target(serve-global - php -S 0.0.0.0:8088 - WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/website - ) -endif() - #----------------------------------------------------------------------------- # Tests #----------------------------------------------------------------------------- diff --git a/docs/admin/Import.md b/docs/admin/Import.md index 21cae89a..280231b6 100644 --- a/docs/admin/Import.md +++ b/docs/admin/Import.md @@ -236,7 +236,8 @@ MB. Make sure you leave enough RAM for PostgreSQL and osm2pgsql as mentioned above. If the system starts swapping or you are getting out-of-memory errors, reduce the cache size or even consider using a flatnode file. -### Verify the import + +### Testing the installation Run this script to verify all required tables and indices got created successfully. @@ -244,12 +245,10 @@ Run this script to verify all required tables and indices got created successful nominatim check-database ``` -### Testing the installation - Now you can try out your installation by running: ```sh -make serve +nominatim serve ``` This runs a small test server normally used for development. You can use it diff --git a/nominatim/cli.py b/nominatim/cli.py index 60701bbf..722022b5 100644 --- a/nominatim/cli.py +++ b/nominatim/cli.py @@ -11,7 +11,7 @@ import logging from pathlib import Path from .config import Configuration -from .tools.exec_utils import run_legacy_script, run_api_script +from .tools.exec_utils import run_legacy_script, run_api_script, run_php_server from .db.connection import connect from .db import status from .errors import UsageError @@ -633,6 +633,28 @@ class QueryExport: return run_legacy_script(*params, nominatim_env=args) + +class AdminServe: + """\ + Start a simple web server for serving the API. + + This command starts the built-in PHP webserver to serve the website + from the current project directory. This webserver is only suitable + for testing and develop. Do not use it in production setups! + + By the default, the webserver can be accessed at: http://127.0.0.1:8088 + """ + + @staticmethod + def add_args(parser): + group = parser.add_argument_group('Server arguments') + group.add_argument('--server', default='127.0.0.1:8088', + help='The address the server will listen to.') + + @staticmethod + def run(args): + run_php_server(args.server, args.project_dir / 'website') + STRUCTURED_QUERY = ( ('street', 'housenumber and street'), ('city', 'city, town or village'), @@ -895,6 +917,7 @@ def nominatim(**kwargs): parser.add_subcommand('refresh', UpdateRefresh) parser.add_subcommand('export', QueryExport) + parser.add_subcommand('serve', AdminServe) if kwargs.get('phpcgi_path'): parser.add_subcommand('search', APISearch) diff --git a/nominatim/tools/exec_utils.py b/nominatim/tools/exec_utils.py index e0d048a2..45853163 100644 --- a/nominatim/tools/exec_utils.py +++ b/nominatim/tools/exec_utils.py @@ -89,6 +89,13 @@ def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None, return 0 +def run_php_server(server_address, base_dir): + """ Run the built-in server from the given directory. + """ + subprocess.run(['/usr/bin/env', 'php', '-S', server_address], + cwd=str(base_dir), check=True) + + def run_osm2pgsql(options): """ Run osm2pgsql with the given options. """ diff --git a/test/python/test_cli.py b/test/python/test_cli.py index cde84759..702a4b74 100644 --- a/test/python/test_cli.py +++ b/test/python/test_cli.py @@ -215,6 +215,14 @@ def test_replication_update_continuous_no_change(monkeypatch, temp_db_conn, stat assert sleep_mock.last_args[0] == 60 +def test_serve_command(monkeypatch): + func = MockParamCapture() + monkeypatch.setattr(nominatim.cli, 'run_php_server', func) + + call_nominatim('serve') + + assert func.called == 1 + @pytest.mark.parametrize("params", [ ('search', '--query', 'new'), ('reverse', '--lat', '0', '--lon', '0'),