Check in current backend
This commit is contained in:
@ -1,21 +1,49 @@
|
||||
"""Settings management."""
|
||||
|
||||
from pydantic import Field
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Any
|
||||
from pydantic import Field, field_validator
|
||||
from pydantic_settings import (
|
||||
BaseSettings,
|
||||
SettingsConfigDict,
|
||||
ForceDecode,
|
||||
)
|
||||
from sqlalchemy import URL
|
||||
|
||||
|
||||
DEFAULT_DATABASE = "sqlite:///sshecret.db"
|
||||
DEFAULT_DATABASE = "sshecret.db"
|
||||
|
||||
|
||||
class BackendSettings(BaseSettings):
|
||||
"""Backend settings."""
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".backend.env", env_prefix="sshecret_")
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".backend.env", env_prefix="sshecret_backend_"
|
||||
)
|
||||
|
||||
db_url: str = Field(default=DEFAULT_DATABASE)
|
||||
database: str = Field(default=DEFAULT_DATABASE)
|
||||
generate_initial_tokens: Annotated[bool, ForceDecode] = Field(default=False)
|
||||
|
||||
@field_validator("generate_initial_tokens", mode="before")
|
||||
@classmethod
|
||||
def cast_bool(cls, value: Any) -> bool:
|
||||
"""Ensure we catch the boolean."""
|
||||
if isinstance(value, str):
|
||||
if value.lower() in ("1", "true", "on"):
|
||||
return True
|
||||
if value.lower() in ("0", "false", "off"):
|
||||
return False
|
||||
return bool(value)
|
||||
|
||||
@property
|
||||
def db_url(self) -> URL:
|
||||
"""Construct database url."""
|
||||
return URL.create(drivername="sqlite", database=self.database)
|
||||
|
||||
@property
|
||||
def db_exists(self) -> bool:
|
||||
"""Check if databatase exists."""
|
||||
return Path(self.database).exists()
|
||||
|
||||
|
||||
def get_settings() -> BackendSettings:
|
||||
|
||||
Reference in New Issue
Block a user