import asyncio
import json
from typing import Dict

from redis import asyncio as aioredis
import uvicorn
from fastapi import FastAPI, BackgroundTasks, UploadFile

app = FastAPI()


async def add_stream(data: dict):
    redis = await aioredis.Redis(host="127.0.0.1", port=6379)
    await redis.lpush("hik-sub-event", json.dumps(data))
    await redis.close()


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.post("/eventRcv")
async def event_rcv(data: dict, background_tasks: BackgroundTasks):
    background_tasks.add_task(add_stream, data)
    return {"msg": "success"}


def run_app():
    uvicorn.run("main:app", host="0.0.0.0", port=8000)


if __name__ == "__main__":
    run_app()
