From 849da61a53f9b8a1cd3c66894ec84df32257be7f Mon Sep 17 00:00:00 2001 From: Rodolfo Olivieri Date: Mon, 21 Oct 2024 16:29:01 -0300 Subject: [PATCH] Refactor start_uvicorn to be less verbose with conditionals Simplified the condition to start uvicorn. The TLS flags will be None in case they are not being used, so there is no need for branching that. The condition was moved to determinate the port of the running service. --- runner.py | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/runner.py b/runner.py index 73a6bc19..f4581b47 100644 --- a/runner.py +++ b/runner.py @@ -113,30 +113,25 @@ def start_uvicorn(): if config.dev_config.run_on_localhost else "0.0.0.0" # noqa: S104 # nosec: B104 ) + port = 8080 if config.dev_config.disable_tls else 8443 log_level = config.ols_config.logging_config.uvicorn_log_level - if config.dev_config.disable_tls: - # TLS is disabled, run without SSL configuration - uvicorn.run( - "ols.app.main:app", - host=host, - port=8080, - log_level=log_level, - workers=1, - access_log=log_level < logging.INFO, - ) - else: - uvicorn.run( - "ols.app.main:app", - host=host, - port=8443, - workers=1, - log_level=log_level, - ssl_keyfile=config.ols_config.tls_config.tls_key_path, - ssl_certfile=config.ols_config.tls_config.tls_certificate_path, - ssl_keyfile_password=config.ols_config.tls_config.tls_key_password, - access_log=log_level < logging.INFO, - ) + # The tls fields can be None, which means we will pass those values as None to uvicorn.run + ssl_keyfile = config.ols_config.tls_config.tls_key_path + ssl_certfile = config.ols_config.tls_config.tls_certificate_path + ssl_keyfile_password = config.ols_config.tls_config.tls_key_password + + uvicorn.run( + "ols.app.main:app", + host=host, + port=port, + workers=1, + log_level=log_level, + ssl_keyfile=ssl_keyfile, + ssl_certfile=ssl_certfile, + ssl_keyfile_password=ssl_keyfile_password, + access_log=log_level < logging.INFO, + ) if __name__ == "__main__":