Skip to content
Snippets Groups Projects
Verified Commit ba1132fb authored by Stefan Hynek's avatar Stefan Hynek :drooling_face:
Browse files

refactor(config): wrap all settings that can be configured from the...

refactor(config): wrap all settings that can be configured from the environment in an AppConfig object
parent fd08709a
No related branches found
No related tags found
1 merge request!24Resolve "make host, port, dsn and textgrid host configurable"
Pipeline #342885 passed
...@@ -3,63 +3,51 @@ ...@@ -3,63 +3,51 @@
# SPDX-License-Identifier: CC0-1.0 # SPDX-License-Identifier: CC0-1.0
import os import os
from typing import Optional
from urllib.parse import urlparse
from .errors import EnvNotSetError
# TODO: remove "tg_auth_wsdl", "tg_auth_address", "tg_nav_address" def check_url(url):
"""Check if `url` can be parsed."""
if url:
result = urlparse(url)
if result.scheme and result.netloc:
return url
raise ValueError(f"{url} is not a valid URL.")
def lookup_env_name(internal_name: str) -> str: def check_port(port):
mapping = { """Check if port is set correctly to a non-privileged value."""
"_auth_wsdl": "tg_auth_wsdl", if 1000 <= port <= 65535:
"_auth_address": "tg_auth_address", return port
"_dsn": "sentry_dsn", raise ValueError(f"{port} is not a valid Port.")
"_host": "tg_host",
"_nav_address": "tg_nav_address",
}
return mapping[internal_name]
# TODO check for trailing "/", add if missing!
# TODO check URLs for validity
class AppConfig: class AppConfig:
# TODO: configure the app according to the set environment (i.e. prod or dev) """Settings that can be be configured from the environment."""
def __init__(self) -> None:
self._dsn = os.getenv(lookup_env_name("_dsn"))
@property
def dsn(self) -> str:
return self._dsn
class TextgridConfig:
def __init__(self) -> None: def __init__(self) -> None:
self._auth_wsdl = os.getenv(lookup_env_name("_auth_wsdl")) self._dsn = check_url(os.getenv("SENTRY_DSN"))
self._auth_address = os.getenv(lookup_env_name("_auth_address")) self._host = os.getenv("REPDAV_HOST") or "localhost"
self._nav_address = os.getenv(lookup_env_name("_nav_address")) self._port = check_port(int(os.getenv("REPDAV_PORT") or 8080))
self._host = os.getenv(lookup_env_name("_host")) self._tg_host = check_url(os.getenv("TEXTGRID_HOST"))
@property @property
def auth_wsdl(self) -> str: def dsn(self) -> Optional[str]:
if self._auth_wsdl: """Sentry DSN."""
return self._auth_wsdl return self._dsn
raise EnvNotSetError(lookup_env_name("_auth_wsdl"))
@property @property
def auth_address(self) -> str: def host(self) -> str:
if self._auth_address: """Service hostname."""
return self._auth_address return self._host
raise EnvNotSetError(lookup_env_name("_auth_address"))
@property @property
def nav_address(self) -> str: def port(self) -> int:
if self._nav_address: """Service port."""
return self._nav_address return self._port
raise EnvNotSetError(lookup_env_name("_nav_address"))
@property @property
def host(self) -> str: def tg_host(self) -> Optional[str]:
if self._host: """Textgrid host URL."""
return self._host return self._tg_host
raise EnvNotSetError(lookup_env_name("_host"))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment