From 7c0ef155678b0dba879e4ffad9f5e7c2f77f93d4 Mon Sep 17 00:00:00 2001 From: Piotr Dec Date: Fri, 17 Oct 2025 20:41:52 +0200 Subject: [PATCH] fix: front-end autowire --- app/api/v1.py | 9 ++++++++- app/main.py | 2 +- app/services/passwords.py | 2 +- app/web/passwords.py | 13 +++++-------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/api/v1.py b/app/api/v1.py index 31427e8..b1785a7 100644 --- a/app/api/v1.py +++ b/app/api/v1.py @@ -1,6 +1,7 @@ from fastapi import APIRouter, Depends, Body, Query from fastapi.responses import JSONResponse from fastapi_utils.cbv import cbv +from injectable import inject from app.api.models import * from app.web import PasswordsController @@ -17,14 +18,20 @@ async def root(): async def health(): return {"status": "ok"} + @router.post("/ci", summary="CI Webhook") async def ci(request: Request): return Response(200) +class AutowireSupport: + @staticmethod + def password_controller(): + return inject(PasswordsController) + @cbv(router) class TreeController: - svc: PasswordsController = Depends(PasswordsController.dep) + svc: PasswordsController = Depends(AutowireSupport.password_controller) @router.get("/tree", response_model=NodeDTO) def get_tree(self) -> NodeDTO: diff --git a/app/main.py b/app/main.py index f173a4a..03f6c34 100644 --- a/app/main.py +++ b/app/main.py @@ -13,6 +13,7 @@ class KarlApplication: from starlette.types import Receive, Scope, Send def __init__(self) -> None: self._set_logging() + load_injection_container() _instance = FastAPI(title="Karl", version="0.1.0") self._set_routes(_instance) self._set_events(_instance) @@ -52,7 +53,6 @@ class KarlApplication: def _init_services(self): logger = logging.getLogger(__name__) - load_injection_container() webhook_service = WebhookProcessor() logger.info(webhook_service.health) diff --git a/app/services/passwords.py b/app/services/passwords.py index fea898c..9b9412a 100644 --- a/app/services/passwords.py +++ b/app/services/passwords.py @@ -12,7 +12,7 @@ class Passwords: settings = get_settings() with open(settings.kp.secret, "r") as fh: - secret = fh.read() + secret = fh.read().splitlines()[0] self._path = settings.kp.file self._kp_org = self._open_or_create(self._path, secret) self._kp = self._open_lock(self._path, secret) diff --git a/app/web/passwords.py b/app/web/passwords.py index cd2c76b..b0e3d51 100644 --- a/app/web/passwords.py +++ b/app/web/passwords.py @@ -1,20 +1,17 @@ -from typing import Optional, List +from typing import Optional, List, Annotated +from injectable import autowired, injectable, Autowired from pykeepass import Group, Entry from app.api.models import EntryKind, EntrySimpleDTO, EntryComplexDTO, GroupDTO, NodeDTO, EntryNodeDTO from app.services import Passwords - +@injectable(singleton=True) class PasswordsController: - def __init__(self, passwords: Passwords): + @autowired + def __init__(self, passwords: Annotated[Passwords, Autowired]): self._pw = passwords - @staticmethod - def dep() -> "PasswordsController": - # prosta fabryka/DI mostkujÄ…ca injectable - return PasswordsController(Passwords()) - # Helpers def _group_by_path(self, path: Optional[str]) -> Group: if not path or path == "/":