Event bus basics

This commit is contained in:
Piotr Dec 2025-10-31 00:01:45 +01:00
parent 1440ec51b7
commit 87e8af3f72
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
8 changed files with 88 additions and 16 deletions

View file

@ -1,4 +1,4 @@
from automapper import mapper
from automapper import mapper, exceptions
from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from starlette.responses import JSONResponse, Response
@ -6,6 +6,7 @@ from starlette.responses import JSONResponse, Response
from app.api.models import Request
from app.core.core import WebhookProcessor
from app.core.injects import AutowireSupport
from app.events import SimpleEventBus
from app.model.webhook import WebhookEvent
router = APIRouter()
@ -19,16 +20,21 @@ async def root():
@cbv(router)
class APIv1:
webhook_service: WebhookProcessor = Depends(AutowireSupport.webhook_processor)
event_bus: SimpleEventBus = Depends(AutowireSupport.event_bus)
logger = __import__('logging').getLogger(__name__)
def __init__(self):
mapper.add(Request, WebhookEvent)
try: # TODO: rejestracja w innym miejscu: klasa jest przeładowywana co żądanie
mapper.add(Request, WebhookEvent)
except exceptions.DuplicatedRegistrationError:
pass
@router.get("/health", summary="Health check")
async def health(self) -> JSONResponse:
return JSONResponse({"status": self.webhook_service.health})
# TODO: JSON serialize
return JSONResponse({"status": self.webhook_service.health.healthy})
@router.post("/ci", summary="CI Webhook")
async def ci(self, request: Request):
self.webhook_service.enqueue(mapper.map(request))
self.event_bus.publish(mapper.map(request))
return Response(status_code=201)