fix: front-end autowire

This commit is contained in:
Piotr Dec 2025-10-17 20:41:52 +02:00
parent 5224fe78b6
commit 7c0ef15567
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
4 changed files with 15 additions and 11 deletions

View file

@ -1,6 +1,7 @@
from fastapi import APIRouter, Depends, Body, Query from fastapi import APIRouter, Depends, Body, Query
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi_utils.cbv import cbv from fastapi_utils.cbv import cbv
from injectable import inject
from app.api.models import * from app.api.models import *
from app.web import PasswordsController from app.web import PasswordsController
@ -17,14 +18,20 @@ async def root():
async def health(): async def health():
return {"status": "ok"} return {"status": "ok"}
@router.post("/ci", summary="CI Webhook") @router.post("/ci", summary="CI Webhook")
async def ci(request: Request): async def ci(request: Request):
return Response(200) return Response(200)
class AutowireSupport:
@staticmethod
def password_controller():
return inject(PasswordsController)
@cbv(router) @cbv(router)
class TreeController: class TreeController:
svc: PasswordsController = Depends(PasswordsController.dep) svc: PasswordsController = Depends(AutowireSupport.password_controller)
@router.get("/tree", response_model=NodeDTO) @router.get("/tree", response_model=NodeDTO)
def get_tree(self) -> NodeDTO: def get_tree(self) -> NodeDTO:

View file

@ -13,6 +13,7 @@ class KarlApplication:
from starlette.types import Receive, Scope, Send from starlette.types import Receive, Scope, Send
def __init__(self) -> None: def __init__(self) -> None:
self._set_logging() self._set_logging()
load_injection_container()
_instance = FastAPI(title="Karl", version="0.1.0") _instance = FastAPI(title="Karl", version="0.1.0")
self._set_routes(_instance) self._set_routes(_instance)
self._set_events(_instance) self._set_events(_instance)
@ -52,7 +53,6 @@ class KarlApplication:
def _init_services(self): def _init_services(self):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
load_injection_container()
webhook_service = WebhookProcessor() webhook_service = WebhookProcessor()
logger.info(webhook_service.health) logger.info(webhook_service.health)

View file

@ -12,7 +12,7 @@ class Passwords:
settings = get_settings() settings = get_settings()
with open(settings.kp.secret, "r") as fh: with open(settings.kp.secret, "r") as fh:
secret = fh.read() secret = fh.read().splitlines()[0]
self._path = settings.kp.file self._path = settings.kp.file
self._kp_org = self._open_or_create(self._path, secret) self._kp_org = self._open_or_create(self._path, secret)
self._kp = self._open_lock(self._path, secret) self._kp = self._open_lock(self._path, secret)

View file

@ -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 pykeepass import Group, Entry
from app.api.models import EntryKind, EntrySimpleDTO, EntryComplexDTO, GroupDTO, NodeDTO, EntryNodeDTO from app.api.models import EntryKind, EntrySimpleDTO, EntryComplexDTO, GroupDTO, NodeDTO, EntryNodeDTO
from app.services import Passwords from app.services import Passwords
@injectable(singleton=True)
class PasswordsController: class PasswordsController:
def __init__(self, passwords: Passwords): @autowired
def __init__(self, passwords: Annotated[Passwords, Autowired]):
self._pw = passwords self._pw = passwords
@staticmethod
def dep() -> "PasswordsController":
# prosta fabryka/DI mostkująca injectable
return PasswordsController(Passwords())
# Helpers # Helpers
def _group_by_path(self, path: Optional[str]) -> Group: def _group_by_path(self, path: Optional[str]) -> Group:
if not path or path == "/": if not path or path == "/":