Spaces:
Runtime error
Runtime error
Create app/main.py
Browse files- app/main.py +38 -0
app/main.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
from contextlib import asynccontextmanager
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
|
| 7 |
+
from app.config import load_config
|
| 8 |
+
from app.cf_api import CFClient
|
| 9 |
+
from bot.client import build_bot
|
| 10 |
+
from bot.handlers import register_handlers
|
| 11 |
+
|
| 12 |
+
cfg = load_config()
|
| 13 |
+
cf = CFClient(cfg.WORKER1_URL, cfg.WORKER2_URL, cfg.BOT_BACKEND_KEY, cfg.HF_API_KEY)
|
| 14 |
+
bot = build_bot(cfg)
|
| 15 |
+
|
| 16 |
+
@asynccontextmanager
|
| 17 |
+
async def lifespan(app: FastAPI):
|
| 18 |
+
# start bot
|
| 19 |
+
await register_handlers(bot, cfg, cf)
|
| 20 |
+
await bot.start()
|
| 21 |
+
yield
|
| 22 |
+
# stop bot
|
| 23 |
+
await bot.stop()
|
| 24 |
+
await cf.close()
|
| 25 |
+
|
| 26 |
+
app = FastAPI(lifespan=lifespan)
|
| 27 |
+
|
| 28 |
+
@app.get("/")
|
| 29 |
+
async def root():
|
| 30 |
+
return {
|
| 31 |
+
"ok": True,
|
| 32 |
+
"service": "yt-uploader-bot",
|
| 33 |
+
"bot_username": cfg.BOT_USERNAME,
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
@app.get("/health")
|
| 37 |
+
async def health():
|
| 38 |
+
return {"ok": True}
|