diff --git a/packetserver/http/config.py b/packetserver/http/config.py index 2bbb273..2e542cc 100644 --- a/packetserver/http/config.py +++ b/packetserver/http/config.py @@ -15,4 +15,4 @@ class Settings(BaseSettings): model_config = SettingsConfigDict( case_sensitive=False, # Make environment variable names case-sensitive env_prefix="PS_APP_" # Use a prefix for environment variables (e.g., MY_APP_DATABASE_URL) - ) \ No newline at end of file + ) diff --git a/packetserver/http/database.py b/packetserver/http/database.py index 0334bec..1f30368 100644 --- a/packetserver/http/database.py +++ b/packetserver/http/database.py @@ -1,13 +1,18 @@ +from copy import deepcopy + from fastapi import Depends from typing import Annotated, Generator from os.path import isfile import ZEO import ZODB +import json from ZODB.Connection import Connection import transaction +import logging from .config import Settings # assuming Settings has zeo_file: str +from ..common.util import convert_from_persistent settings = Settings() @@ -63,4 +68,12 @@ def get_transaction_manager(): # Annotated dependencies for routers DbDependency = Annotated[ZODB.DB, Depends(get_db)] -#ConnectionDependency = Annotated[Connection, Depends(get_connection)] \ No newline at end of file +#ConnectionDependency = Annotated[Connection, Depends(get_connection)] + +def get_server_config_from_db(db: DbDependency) -> dict: + with db.transaction() as conn: + db_config = convert_from_persistent(conn.root.config) + if type(db_config) is not dict: + raise RuntimeError("The config property is not a dict.") + db_config['server_callsign'] = conn.root.server_callsign + return db_config \ No newline at end of file diff --git a/packetserver/http/server.py b/packetserver/http/server.py index ea332d3..c1ca350 100644 --- a/packetserver/http/server.py +++ b/packetserver/http/server.py @@ -5,7 +5,7 @@ from fastapi.templating import Jinja2Templates from pathlib import Path import base64 -from .database import init_db +from .database import init_db, get_db, get_server_config_from_db from .routers import public, profile, messages, send from .logging import init_logging @@ -22,6 +22,8 @@ app = FastAPI( # Define templates EARLY (before importing dashboard) templates = Jinja2Templates(directory=BASE_DIR / "templates") + + def b64decode_filter(value: str) -> str: try: decoded_bytes = base64.b64decode(value) @@ -60,6 +62,12 @@ from .routers.jobs import dashboard_router as jobs_html_router # initialize database init_db() +db = get_db() +server_config = get_server_config_from_db(db) +templates.env.globals['server_name'] = server_config['server_name'] +templates.env.globals['server_callsign'] = server_config['server_callsign'] +templates.env.globals['motd'] = server_config['motd'] +templates.env.globals['server_operator'] = server_config['operator'] # Include routers app.include_router(public.router) diff --git a/packetserver/http/templates/base.html b/packetserver/http/templates/base.html index f946921..ab834a4 100644 --- a/packetserver/http/templates/base.html +++ b/packetserver/http/templates/base.html @@ -10,7 +10,7 @@