foxhole/app/config.py
2023-04-07 14:25:08 +08:00

70 lines
1.7 KiB
Python

from pathlib import Path
import tomli
import pydantic
DEBUG = True
MAIN_VERSION = "0.1.1"
ROOT_DIR = Path().parent.resolve()
DB_PATH = ROOT_DIR / "data" / "database.db"
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
_CONFIG_FILE = "config.toml"
class Config(pydantic.BaseModel):
domain: str
username: str
# admin_password: bytes # TODO admin password
name: str
summary: str
https: bool
icon_url: str
image_url: str | None = None
secret: str
debug: bool = False
trusted_hosts: list[str] = ["127.0.0.1"]
manually_approves_followers: bool = False
also_known_as: str | None = None
hides_followers: bool = False
hides_following: bool = False
inbox_retention_days: int = 15
post_token: str | None = None
def load_config() -> Config:
try:
return Config.parse_obj(
tomli.loads((ROOT_DIR / "data" / _CONFIG_FILE).read_text())
)
except FileNotFoundError:
raise ValueError(
f"{_CONFIG_FILE} is missing"
)
def get_version_commit() -> str:
import subprocess
try:
return (
'+' +
subprocess.check_output(["git", "rev-parse", "--short=8", "HEAD"])
.split()[0]
.decode()
)
except Exception:
return "+dev"
CONFIG = load_config()
DOMAIN = CONFIG.domain
_SCHEME = "https" if CONFIG.https else "http"
ID = f"{_SCHEME}://{DOMAIN}"
BASE_URL = ID
USERNAME = CONFIG.username
KEY_PATH = (ROOT_DIR / "data" / "key.pem")
VERSION = "Foxhole-"+ MAIN_VERSION + get_version_commit()
USER_AGENT = "Fediverse Application/" + VERSION
AP_CONTENT_TYPE = "application/activity+json"
MANUALLY_APPROVES_FOLLOWERS = CONFIG.manually_approves_followers
POST_TOKEN = CONFIG.post_token