feat(shared): add config trailing slash sanitizer (#1870)
This commit is contained in:
parent
8ab103ab44
commit
3705747132
2 changed files with 33 additions and 4 deletions
|
@ -1,19 +1,23 @@
|
|||
import sys
|
||||
from os import environ
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
|
||||
|
||||
from loguru import logger
|
||||
|
||||
# pylint: disable=no-name-in-module
|
||||
from pydantic import AnyHttpUrl, BaseModel, ValidationError
|
||||
from pydantic import AnyHttpUrl, BaseModel, ValidationError, validator
|
||||
from pydantic.fields import ModelField
|
||||
from pydantic.utils import deep_update
|
||||
from yaml import YAMLError, safe_load
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pydantic.typing import AnyClassMethod
|
||||
|
||||
DEFAULT_ENV_PREFIX = "LIBRETIME"
|
||||
DEFAULT_CONFIG_FILEPATH = Path("/etc/libretime/config.yml")
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class BaseConfig(BaseModel):
|
||||
"""
|
||||
|
@ -103,16 +107,30 @@ class BaseConfig(BaseModel):
|
|||
return {}
|
||||
|
||||
|
||||
def no_trailing_slash_validator(key: str) -> "AnyClassMethod":
|
||||
# pylint: disable=unused-argument
|
||||
def strip_trailing_slash(cls: Any, value: str) -> str:
|
||||
return value.rstrip("/")
|
||||
|
||||
return validator(key, pre=True, allow_reuse=True)(strip_trailing_slash)
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class GeneralConfig(BaseModel):
|
||||
public_url: AnyHttpUrl
|
||||
api_key: str
|
||||
|
||||
# Validators
|
||||
_public_url_no_trailing_slash = no_trailing_slash_validator("public_url")
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class StorageConfig(BaseModel):
|
||||
path: str = "/srv/libretime"
|
||||
|
||||
# Validators
|
||||
_path_no_trailing_slash = no_trailing_slash_validator("path")
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class DatabaseConfig(BaseModel):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue