backend/med_backend/settings.py

102 lines
2.3 KiB
Python

import enum
from pathlib import Path
from tempfile import gettempdir
from typing import Optional
from pydantic import BaseSettings
from yarl import URL
TEMP_DIR = Path(gettempdir())
class LogLevel(str, enum.Enum): # noqa: WPS600
"""Possible log levels."""
NOTSET = "NOTSET"
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
FATAL = "FATAL"
class Settings(BaseSettings):
"""
Application settings.
These parameters can be configured
with environment variables.
"""
host: str = "127.0.0.1"
port: int = 8000
# quantity of workers for uvicorn
workers_count: int = 1
# Enable uvicorn reloading
reload: bool = False
JWT_ALGORITHM: str = "HS256"
# TODO: FIX FOR PROD
JWT_EAT: int = 60 * 24 * 10
# Current environment
environment: str = "dev"
log_level: LogLevel = LogLevel.INFO
# Variables for the database
db_host: str = "127.0.0.1"
db_port: int = 5432
db_user: str = "postgres"
db_pass: str = "postgres"
db_base: str = "med_backend"
db_echo: bool = False
# Variables for Redis
redis_host: str = "med_backend-redis"
redis_port: int = 6379
redis_user: Optional[str] = None
redis_pass: Optional[str] = None
redis_base: Optional[int] = None
@property
def db_url(self) -> URL:
"""
Assemble database URL from settings.
:return: database URL.
"""
return URL.build(
scheme="postgresql+asyncpg",
host=self.db_host,
port=self.db_port,
user=self.db_user,
password=self.db_pass,
path=f"/{self.db_base}",
)
@property
def redis_url(self) -> URL:
"""
Assemble REDIS URL from settings.
:return: redis URL.
"""
path = ""
if self.redis_base is not None:
path = f"/{self.redis_base}"
return URL.build(
scheme="redis",
host=self.redis_host,
port=self.redis_port,
user=self.redis_user,
password=self.redis_pass,
path=path,
)
class Config:
env_file = ".env"
env_prefix = "MED_BACKEND_"
env_file_encoding = "utf-8"
settings = Settings()