diff --git a/pyproject.toml b/pyproject.toml index fb29fd0..da5436b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,9 @@ dev = [ "bandit>=1.8.3" ] +[project.scripts] +xmpp-api-useradd = "xmpp_api.cli.add-user:main" + [build-system] requires = ["hatchling"] build-backend = "hatchling.build" diff --git a/src/xmpp_api/cli/add-user.py b/src/xmpp_api/cli/add-user.py index 67f3641..46d64c5 100644 --- a/src/xmpp_api/cli/add-user.py +++ b/src/xmpp_api/cli/add-user.py @@ -1,4 +1,5 @@ import uuid +import argparse from sqlmodel import SQLModel @@ -9,6 +10,14 @@ from xmpp_api.util.token import generate_token def main(): + """ + Creates a new user that can create bots + """ + + parser = argparse.ArgumentParser() + parser.add_argument("--name", required=True, help="Name of the user") + args = parser.parse_args() + # Create all tables engine = get_engine(load_config()) SQLModel.metadata.create_all(engine) @@ -17,14 +26,10 @@ def main(): session = next(get_session(engine)) user = User( id=uuid.uuid4().hex, - name="alexander", + name=args.name, token=generate_token(64), ) session.add(user) session.commit() print(f"User token: {user.token}") - - -if __name__ == "__main__": - main() diff --git a/src/xmpp_api/config/config.py b/src/xmpp_api/config/config.py index abefefa..69cfb47 100644 --- a/src/xmpp_api/config/config.py +++ b/src/xmpp_api/config/config.py @@ -1,6 +1,9 @@ +import os from typing import Annotated +import logging from pydantic import BaseModel +from pydantic_yaml import parse_yaml_file_as from fastapi import Depends @@ -23,19 +26,21 @@ class _Config(BaseModel): component: _ComponentConfig +log = logging.getLogger("config") + + def load_config() -> _Config: """ Load the application config """ - # TODO: Actually load it - return _Config( - database="sqlite:///db.sqlite3", - component=_ComponentConfig( - jid="test.localhost", - server="localhost:5869", - secret="abc123", - ), - ) + + config_path = "/etc/xmpp-api/config.yaml" + if (env_config_file := os.environ.get("XMPP_API__CONFIG_FILE")) is not None: + config_path = env_config_file + + log.info(f"Reading configuration from {config_path}") + with open(config_path, "r", encoding="utf8") as f: + return parse_yaml_file_as(_Config, f) ConfigDep = Annotated[_Config, Depends(load_config)] diff --git a/src/xmpp_api/main.py b/src/xmpp_api/main.py index cc1b14f..97ef762 100644 --- a/src/xmpp_api/main.py +++ b/src/xmpp_api/main.py @@ -1,3 +1,6 @@ +import os +import logging + from fastapi import FastAPI from sqlmodel import SQLModel @@ -13,6 +16,15 @@ app.include_router(api_v1.router) @app.on_event("startup") def startup(): + # Setup logging + logging.basicConfig( + level=( + logging.DEBUG + if os.environ.get("XMPP_API__LOG_LEVEL") == "DEBUG" + else logging.INFO + ), + ) + # TODO: This is kinda ugly config = load_config() engine = app.dependency_overrides.get(get_engine, get_engine)(config)