Add basic logging
Some checks failed
ci/woodpecker/push/check Pipeline failed

This commit is contained in:
PapaTutuWawa 2025-04-21 13:32:38 +02:00
parent 4be01df4da
commit 98a329ed74
4 changed files with 39 additions and 14 deletions

View File

@ -25,6 +25,9 @@ dev = [
"bandit>=1.8.3" "bandit>=1.8.3"
] ]
[project.scripts]
xmpp-api-useradd = "xmpp_api.cli.add-user:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
build-backend = "hatchling.build" build-backend = "hatchling.build"

View File

@ -1,4 +1,5 @@
import uuid import uuid
import argparse
from sqlmodel import SQLModel from sqlmodel import SQLModel
@ -9,6 +10,14 @@ from xmpp_api.util.token import generate_token
def main(): 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 # Create all tables
engine = get_engine(load_config()) engine = get_engine(load_config())
SQLModel.metadata.create_all(engine) SQLModel.metadata.create_all(engine)
@ -17,14 +26,10 @@ def main():
session = next(get_session(engine)) session = next(get_session(engine))
user = User( user = User(
id=uuid.uuid4().hex, id=uuid.uuid4().hex,
name="alexander", name=args.name,
token=generate_token(64), token=generate_token(64),
) )
session.add(user) session.add(user)
session.commit() session.commit()
print(f"User token: {user.token}") print(f"User token: {user.token}")
if __name__ == "__main__":
main()

View File

@ -1,6 +1,9 @@
import os
from typing import Annotated from typing import Annotated
import logging
from pydantic import BaseModel from pydantic import BaseModel
from pydantic_yaml import parse_yaml_file_as
from fastapi import Depends from fastapi import Depends
@ -23,19 +26,21 @@ class _Config(BaseModel):
component: _ComponentConfig component: _ComponentConfig
log = logging.getLogger("config")
def load_config() -> _Config: def load_config() -> _Config:
""" """
Load the application config Load the application config
""" """
# TODO: Actually load it
return _Config( config_path = "/etc/xmpp-api/config.yaml"
database="sqlite:///db.sqlite3", if (env_config_file := os.environ.get("XMPP_API__CONFIG_FILE")) is not None:
component=_ComponentConfig( config_path = env_config_file
jid="test.localhost",
server="localhost:5869", log.info(f"Reading configuration from {config_path}")
secret="abc123", with open(config_path, "r", encoding="utf8") as f:
), return parse_yaml_file_as(_Config, f)
)
ConfigDep = Annotated[_Config, Depends(load_config)] ConfigDep = Annotated[_Config, Depends(load_config)]

View File

@ -1,3 +1,6 @@
import os
import logging
from fastapi import FastAPI from fastapi import FastAPI
from sqlmodel import SQLModel from sqlmodel import SQLModel
@ -13,6 +16,15 @@ app.include_router(api_v1.router)
@app.on_event("startup") @app.on_event("startup")
def 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 # TODO: This is kinda ugly
config = load_config() config = load_config()
engine = app.dependency_overrides.get(get_engine, get_engine)(config) engine = app.dependency_overrides.get(get_engine, get_engine)(config)