import threading import uvicorn from fastapi import FastAPI from app.api.v1.api import api_router from app.core.config import settings from app.kafka_consumer import start_consumer_loop app = FastAPI(title=settings.PROJECT_NAME) app.include_router(api_router, prefix="/api/v1") @app.on_event("startup") def startup_event(): """ On startup, launch the Kafka consumer in a daemon background thread so the event loop and the HTTP server run concurrently. """ consumer_thread = threading.Thread( target=start_consumer_loop, daemon=True, name="kafka-consumer", ) consumer_thread.start() @app.get("/health") def health_check(): return {"status": "working"} if __name__ == "__main__": uvicorn.run("app.main:app", host="127.0.0.1", port=8001, reload=True)