diff --git a/setup.cfg b/setup.cfg index b475dba..8c01374 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,3 +35,4 @@ where = src console_scripts = capport-control = capport.control.run:main capport-stats = capport.stats:main + capport-webui = capport.api.hypercorn_run:main diff --git a/src/capport/api/hypercorn_conf.py b/src/capport/api/hypercorn_conf.py deleted file mode 100644 index 4661c0a..0000000 --- a/src/capport/api/hypercorn_conf.py +++ /dev/null @@ -1,12 +0,0 @@ -from __future__ import annotations - -import capport.config - -_config = capport.config.Config.load_default_once() - -worker_class = 'trio' - -if _config.server_names: - server_names = _config.server_names -elif not _config.debug: - raise Exception("production setup requires server-names in config (list of accepted hostnames in http requests)") diff --git a/src/capport/api/hypercorn_run.py b/src/capport/api/hypercorn_run.py new file mode 100644 index 0000000..c922fff --- /dev/null +++ b/src/capport/api/hypercorn_run.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +import hypercorn.config +import hypercorn.trio.run +import hypercorn.utils + +import capport.config + + +def run(config: hypercorn.config.Config) -> None: + sockets = config.create_sockets() + assert config.worker_class == 'trio' + + hypercorn.trio.run.trio_worker(config=config, sockets=sockets) + + for sock in sockets.secure_sockets: + sock.close() + for sock in sockets.insecure_sockets: + sock.close() + + +def main() -> None: + _config = capport.config.Config.load_default_once() + + hypercorn_config = hypercorn.config.Config() + hypercorn_config.application_path = 'capport.api.app' + hypercorn_config.worker_class = 'trio' + + if _config.server_names: + hypercorn_config.server_names = _config.server_names + elif not _config.debug: + raise Exception( + "production setup requires server-names in config (list of accepted hostnames in http requests)" + ) + + run(hypercorn_config) diff --git a/start-api.sh b/start-api.sh index 5bb91de..ae69070 100755 --- a/start-api.sh +++ b/start-api.sh @@ -5,4 +5,4 @@ set -e base=$(dirname "$(readlink -f "$0")") cd "${base}" -exec ./venv/bin/hypercorn --config python:capport.api.hypercorn_conf capport.api.app "$@" +exec ./venv/bin/capport-webui "$@"