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 @@
# SPDX-License-Identifier: CC0-1.0
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:
mapping = {
"_auth_wsdl": "tg_auth_wsdl",
"_auth_address": "tg_auth_address",
"_dsn": "sentry_dsn",
"_host": "tg_host",
"_nav_address": "tg_nav_address",
}
return mapping[internal_name]
def check_port(port):
"""Check if port is set correctly to a non-privileged value."""
if 1000 <= port <= 65535:
return port
raise ValueError(f"{port} is not a valid Port.")
# TODO check for trailing "/", add if missing!
# TODO check URLs for validity
class AppConfig:
# TODO: configure the app according to the set environment (i.e. prod or dev)
def __init__(self) -> None:
self._dsn = os.getenv(lookup_env_name("_dsn"))
@property
def dsn(self) -> str:
return self._dsn
"""Settings that can be be configured from the environment."""
class TextgridConfig:
def __init__(self) -> None:
self._auth_wsdl = os.getenv(lookup_env_name("_auth_wsdl"))
self._auth_address = os.getenv(lookup_env_name("_auth_address"))
self._nav_address = os.getenv(lookup_env_name("_nav_address"))
self._host = os.getenv(lookup_env_name("_host"))
self._dsn = check_url(os.getenv("SENTRY_DSN"))
self._host = os.getenv("REPDAV_HOST") or "localhost"
self._port = check_port(int(os.getenv("REPDAV_PORT") or 8080))
self._tg_host = check_url(os.getenv("TEXTGRID_HOST"))
@property
def auth_wsdl(self) -> str:
if self._auth_wsdl:
return self._auth_wsdl
raise EnvNotSetError(lookup_env_name("_auth_wsdl"))
def dsn(self) -> Optional[str]:
"""Sentry DSN."""
return self._dsn
@property
def auth_address(self) -> str:
if self._auth_address:
return self._auth_address
raise EnvNotSetError(lookup_env_name("_auth_address"))
def host(self) -> str:
"""Service hostname."""
return self._host
@property
def nav_address(self) -> str:
if self._nav_address:
return self._nav_address
raise EnvNotSetError(lookup_env_name("_nav_address"))
def port(self) -> int:
"""Service port."""
return self._port
@property
def host(self) -> str:
if self._host:
return self._host
raise EnvNotSetError(lookup_env_name("_host"))
def tg_host(self) -> Optional[str]:
"""Textgrid host URL."""
return self._tg_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