Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,74 +1,38 @@
|
|
| 1 |
# main.py
|
| 2 |
-
import os
|
| 3 |
import logging
|
| 4 |
import asyncio
|
| 5 |
-
|
| 6 |
from fastapi import FastAPI
|
| 7 |
-
from fastapi.responses import
|
| 8 |
import uvicorn
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Setup logging
|
| 13 |
-
logging.basicConfig(
|
| 14 |
-
level=logging.INFO,
|
| 15 |
-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
description="Web interface for checking Terabox Bot status.",
|
| 21 |
-
version="1.0.0",
|
| 22 |
-
)
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
@app.on_event("startup")
|
| 27 |
async def on_startup():
|
| 28 |
-
|
| 29 |
-
asyncio.create_task(
|
| 30 |
-
|
| 31 |
-
# Routes
|
| 32 |
-
@app.get("/", response_class=HTMLResponse)
|
| 33 |
-
async def root():
|
| 34 |
-
return """
|
| 35 |
-
<html>
|
| 36 |
-
<head>
|
| 37 |
-
<title>Terabox Bot - Admin Panel</title>
|
| 38 |
-
</head>
|
| 39 |
-
<body>
|
| 40 |
-
<h2>π Terabox Bot is running!</h2>
|
| 41 |
-
<p>Endpoints:</p>
|
| 42 |
-
<ul>
|
| 43 |
-
<li><a href="/health">/health</a> β Health check</li>
|
| 44 |
-
<li><a href="/status">/status</a> β Bot queue status</li>
|
| 45 |
-
</ul>
|
| 46 |
-
</body>
|
| 47 |
-
</html>
|
| 48 |
-
"""
|
| 49 |
|
| 50 |
@app.get("/health")
|
| 51 |
-
async def
|
| 52 |
-
return {
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
total = batch["total_links"]
|
| 66 |
-
html += f"<li>Batch <b>{batch_id[:6]}</b>: {processed}/{total} processed</li>"
|
| 67 |
-
html += "</ul>"
|
| 68 |
-
|
| 69 |
-
return html
|
| 70 |
|
| 71 |
-
# Run with uvicorn
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
|
| 74 |
-
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=False)
|
|
|
|
| 1 |
# main.py
|
|
|
|
| 2 |
import logging
|
| 3 |
import asyncio
|
|
|
|
| 4 |
from fastapi import FastAPI
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
import uvicorn
|
| 7 |
|
| 8 |
+
import bot # import your bot.py (it already starts Dispatcher there)
|
| 9 |
+
import db_utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
app = FastAPI()
|
| 15 |
|
| 16 |
@app.on_event("startup")
|
| 17 |
async def on_startup():
|
| 18 |
+
logger.info("Starting bot polling in background...")
|
| 19 |
+
asyncio.create_task(bot.run_polling())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.get("/health")
|
| 22 |
+
async def health_check():
|
| 23 |
+
return JSONResponse({
|
| 24 |
+
"status": "ok",
|
| 25 |
+
"workers": bot.config.CONCURRENT_WORKERS,
|
| 26 |
+
"active_batches": len(bot.BATCH_JOBS),
|
| 27 |
+
"active_users": await db_utils.get_all_active_user_ids_db(),
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
@app.get("/")
|
| 31 |
+
async def root_page():
|
| 32 |
+
return JSONResponse({
|
| 33 |
+
"message": "π TeraBox Bot is running.",
|
| 34 |
+
"endpoints": ["/health"],
|
| 35 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
if __name__ == "__main__":
|
| 38 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|