build settings
This commit is contained in:
1
fuware/core/settings/__init__.py
Normal file
1
fuware/core/settings/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .settings import *
|
28
fuware/core/settings/db_providers.py
Normal file
28
fuware/core/settings/db_providers.py
Normal file
@ -0,0 +1,28 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from pydantic import BaseModel
|
||||
|
||||
class AbstractDBProvider(ABC):
|
||||
@property
|
||||
@abstractmethod
|
||||
def db_url(self) -> str: ...
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def db_url_public(self) -> str: ...
|
||||
|
||||
class SQLiteProvider(AbstractDBProvider, BaseModel):
|
||||
data_dir: Path
|
||||
prefix: str = ""
|
||||
|
||||
@property
|
||||
def db_path(self):
|
||||
return self.data_dir / f"{self.prefix}mealie.db"
|
||||
|
||||
@property
|
||||
def db_url(self) -> str:
|
||||
return f"sqlite:///{str(self.db_path.absolute())}"
|
||||
|
||||
@property
|
||||
def db_url_public(self) -> str:
|
||||
return self.db_url
|
71
fuware/core/settings/settings.py
Normal file
71
fuware/core/settings/settings.py
Normal file
@ -0,0 +1,71 @@
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from fuware.core.settings.db_providers import AbstractDBProvider, SQLiteProvider
|
||||
from pydantic_settings import BaseSettings # type: ignore
|
||||
|
||||
|
||||
def determine_secrets(data_dir: Path, production: bool) -> str:
|
||||
if not production:
|
||||
return "shh-secret-test-key"
|
||||
|
||||
secrets_file = data_dir.joinpath(".secret")
|
||||
if secrets_file.is_file():
|
||||
with open(secrets_file) as f:
|
||||
return f.read()
|
||||
else:
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
with open(secrets_file, "w") as f:
|
||||
new_secret = secrets.token_hex(32)
|
||||
f.write(new_secret)
|
||||
return new_secret
|
||||
|
||||
class AppSettings(BaseSettings):
|
||||
PRODUCTION: bool
|
||||
BASE_URL: str = "http://localhost:8080"
|
||||
"""trailing slashes are trimmed (ex. `http://localhost:8080/` becomes ``http://localhost:8080`)"""
|
||||
|
||||
HOST_IP: str = "*"
|
||||
|
||||
API_HOST: str = "0.0.0.0"
|
||||
API_PORT: int = 9000
|
||||
API_DOCS: bool = True
|
||||
|
||||
ALLOW_SIGNUP: bool = False
|
||||
|
||||
@property
|
||||
def DOCS_URL(self) -> str | None:
|
||||
return "/docs" if self.API_DOCS else None
|
||||
|
||||
@property
|
||||
def REDOC_URL(self) -> str | None:
|
||||
return "/redoc" if self.API_DOCS else None
|
||||
|
||||
# ===============================================
|
||||
# Database Configuration
|
||||
|
||||
DB_ENGINE: str = "sqlite" # Options: 'sqlite', 'postgres'
|
||||
DB_PROVIDER: AbstractDBProvider | None = None
|
||||
|
||||
@property
|
||||
def DB_URL(self) -> str | None:
|
||||
return self.DB_PROVIDER.db_url if self.DB_PROVIDER else None
|
||||
|
||||
@property
|
||||
def DB_URL_PUBLIC(self) -> str | None:
|
||||
return self.DB_PROVIDER.db_url_public if self.DB_PROVIDER else None
|
||||
|
||||
def app_settings_constructor(data_dir: Path, production: bool, env_file: Path, env_encoding="utf-8") -> AppSettings:
|
||||
"""
|
||||
app_settings_constructor is a factory function that returns an AppSettings object. It is used to inject the
|
||||
required dependencies into the AppSettings object and nested child objects. AppSettings should not be substantiated
|
||||
directly, but rather through this factory function.
|
||||
"""
|
||||
app_settings = AppSettings(
|
||||
_env_file=env_file, # type: ignore
|
||||
_env_file_encoding=env_encoding, # type: ignore
|
||||
**{"SECRET": determine_secrets(data_dir, production)},
|
||||
)
|
||||
|
||||
app_settings.DB_PROVIDER = SQLiteProvider(data_dir=data_dir)
|
||||
|
||||
return app_settings
|
22
fuware/core/settings/static.py
Normal file
22
fuware/core/settings/static.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from pathlib import Path
|
||||
from fuware import __version__
|
||||
|
||||
load_dotenv()
|
||||
|
||||
APP_VERSION = __version__
|
||||
CWD = Path(__file__).parent
|
||||
BASE_DIR = CWD.parent.parent.parent
|
||||
|
||||
SERCET_KEY = b"oWNhXlfo666JlMHk6UHYxeNB6z_CA2MisDDZJe4N0yc="
|
||||
COOKIE_KEY = os.getenv('VITE_LOGIN_KEY') or '7fo24CMyIc'
|
||||
# URL_DATABASE = "postgresql://{0}:{1}@{2}:{3}/{4}".format(
|
||||
# os.getenv('LOL_DB_USER'),
|
||||
# os.getenv('LOL_DB_PASSWORD'),
|
||||
# os.getenv('LOL_DB_HOST'),
|
||||
# os.getenv('LOL_DB_PORT'),
|
||||
# os.getenv('LOL_DB_NAME'),
|
||||
# )
|
||||
URL_DATABASE = "sqlite:///./test.db"
|
Reference in New Issue
Block a user