diff --git a/README.md b/README.md index c40ad6d913ae6ea091451a71c06fdfd05a9c7b57..eafed1ceb0413a45c5af51283b6a24ca2da9cf96 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,16 @@ SPDX-License-Identifier: CC0-1.0 pip install -r requirements.txt -r requirements.dev.txt ``` -1. Configure the service by setting the following variables either in your environment or in the `docker-compose.yml` file. - - host - - port - - tg_auth_wsdl - - tg_auth_address +1. Configure the service by setting the following variables in your environment. + + Name | Usage / Default + ------|----------------- + **TEXTGRID_URL** | Textgrid repository service URL. Default: **<https://textgridlab.org>** + **REPDAV_HOST** | Service hostname. Default: **localhost** + **REPDAV_LOG_LEVEL** | Service logging level. Default: **WARNING** + **REPDAV_PORT** | Service port. Default: **8080** + **SENTRY_DSN** | Sentry Data Source Name for the Service. If unset, disables Sentry Error Tracking. Default: ***None*** + **SENTRY_ENV** | Sentry environment. Default: **production** 1. Start the service. diff --git a/docker-compose.yml b/docker-compose.yml index d997a5dca8139e8dc9310823b7a8b975afee966a..3bb7fd645f0454e69b9bf7d44ecd4a6146c007bb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,6 +16,7 @@ services: REPDAV_HOST: ${REPDAV_HOST:-0.0.0.0} REPDAV_PORT: ${REPDAV_PORT:-8080} SENTRY_DSN: ${SENTRY_DSN} - TEXTGRID_HOST: ${TEXTGRID_HOST} + TEXTGRID_URL: ${TEXTGRID_URL} + REPDAV_LOG_LEVEL: ${REPDAV_LOG_LEVEL} volumes: - $PWD/src:/app diff --git a/src/main.py b/src/main.py index 3bf7cd8d74c5afa3e07b2c6c66d06eb864a3b7c6..87411dafc0980c084d2a095e00039aba0204c4c4 100644 --- a/src/main.py +++ b/src/main.py @@ -12,18 +12,22 @@ from repdav.config import AppConfig app_config = AppConfig() if app_config.dsn: - sentry_sdk.init(app_config.dsn, traces_sample_rate=1.0) + sentry_sdk.init( + dsn=app_config.dsn, + environment=app_config.environment, + traces_sample_rate=1.0, + ) logging.basicConfig( - level=logging.DEBUG, format="%(name)s %(levelname)s %(asctime)s %(message)s" + level=app_config.log_level, format="%(name)s %(levelname)s %(asctime)s %(message)s" ) _logger = logging.getLogger(__name__) _logger.propagate = True # configure the "wsgidav" named logger -tmp_logger = logging.getLogger("wsgidav") -tmp_logger.propagate = True -tmp_logger.setLevel(logging.DEBUG) +wsgidav_logger = logging.getLogger("wsgidav") +wsgidav_logger.propagate = True +wsgidav_logger.setLevel(app_config.log_level) # Configuration of the WsgiDAVApp. # https://wsgidav.readthedocs.io/en/latest/user_guide_configure.html diff --git a/src/repdav/config.py b/src/repdav/config.py index b3329bff835776b27920ecf623db28404ef786c4..6bb35a4741e53bf18efff5ae8144cda0372c5197 100644 --- a/src/repdav/config.py +++ b/src/repdav/config.py @@ -3,51 +3,79 @@ # SPDX-License-Identifier: CC0-1.0 import os +import logging from typing import Optional from urllib.parse import urlparse +_logger = logging.getLogger(__name__) -def check_url(url): + +def check_url(url: str): """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.") + result = urlparse(url) + if result.scheme and result.netloc: + return url + raise ValueError(f"{url} is not a valid URL.") -def check_port(port): +def check_port(port: int): """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.") +def check_log_level(level: str): + """Check if log level exists.""" + if level in logging._nameToLevel: # pylint: disable=protected-access + return level + _logger.warning("%s is not a valid Logging Level. Falling back to WARNING.", level) + return "WARNING" + + class AppConfig: """Settings that can be be configured from the environment.""" def __init__(self) -> None: - 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")) + _dsn = os.getenv("SENTRY_DSN") + self._dsn = check_url(_dsn) if _dsn else None + _environment = os.getenv("SENTRY_ENV") + self._environment = _environment if _environment else "production" + _host = os.getenv("REPDAV_HOST") + self._host = _host if _host else "localhost" + _log_level = os.getenv("REPDAV_LOG_LEVEL") + self._log_level = check_log_level(_log_level) if _log_level else "WARNING" + _port = os.getenv("REPDAV_PORT") + self._port = check_port(int(_port)) if _port else 8080 + _tg_host = os.getenv("TEXTGRID_URL") + self._tg_host = check_url(_tg_host) if _tg_host else "https://textgridlab.org" @property def dsn(self) -> Optional[str]: """Sentry DSN.""" return self._dsn + @property + def environment(self) -> str: + """Sentry Environment.""" + return self._environment + @property def host(self) -> str: """Service hostname.""" return self._host + @property + def log_level(self) -> str: + """Service log level.""" + return self._log_level + @property def port(self) -> int: """Service port.""" return self._port @property - def tg_host(self) -> Optional[str]: + def tg_host(self) -> str: """Textgrid host URL.""" return self._tg_host