Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,44 +4,49 @@ import logging
|
|
| 4 |
import asyncio
|
| 5 |
import uvicorn
|
| 6 |
from fastapi import FastAPI
|
| 7 |
-
from
|
| 8 |
-
from bot import start_bot
|
| 9 |
|
| 10 |
-
|
| 11 |
-
logging.basicConfig(
|
| 12 |
-
level=logging.INFO,
|
| 13 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 14 |
-
)
|
| 15 |
|
|
|
|
| 16 |
logger = logging.getLogger("main")
|
| 17 |
|
| 18 |
app = FastAPI()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
async def startup_event():
|
| 25 |
logger.info("Starting bot polling in background...")
|
| 26 |
-
asyncio.create_task(dp.start_polling(bot_instance))
|
| 27 |
-
|
| 28 |
-
@app.get("/", response_class=HTMLResponse)
|
| 29 |
-
async def root():
|
| 30 |
-
html_content = """
|
| 31 |
-
<html>
|
| 32 |
-
<head><title>Terabox Bot Status</title></head>
|
| 33 |
-
<body style="font-family:sans-serif;">
|
| 34 |
-
<h1>✅ Terabox Bot is Running</h1>
|
| 35 |
-
<p>You can check /health endpoint too.</p>
|
| 36 |
-
</body>
|
| 37 |
-
</html>
|
| 38 |
-
"""
|
| 39 |
-
return HTMLResponse(content=html_content)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
@app.get("/health")
|
| 42 |
async def health():
|
| 43 |
-
return {"status": "ok", "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
|
| 47 |
-
uvicorn.run("main:app", host="0.0.0.0", port=7860)
|
|
|
|
| 4 |
import asyncio
|
| 5 |
import uvicorn
|
| 6 |
from fastapi import FastAPI
|
| 7 |
+
from contextlib import asynccontextmanager
|
|
|
|
| 8 |
|
| 9 |
+
from bot import start_bot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger("main")
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
+
# Lifespan event for startup/shutdown (new style → no deprecation warning!)
|
| 17 |
+
@asynccontextmanager
|
| 18 |
+
async def lifespan(app: FastAPI):
|
| 19 |
+
logger.info("===== Application Startup =====")
|
| 20 |
+
|
| 21 |
+
# Start bot in background
|
| 22 |
+
dp, bot = start_bot()
|
| 23 |
|
| 24 |
+
asyncio.create_task(dp.start_polling(bot))
|
|
|
|
| 25 |
logger.info("Starting bot polling in background...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
yield
|
| 28 |
+
|
| 29 |
+
logger.info("===== Application Shutdown =====")
|
| 30 |
+
await bot.session.close()
|
| 31 |
+
|
| 32 |
+
# Set FastAPI lifespan
|
| 33 |
+
app.router.lifespan_context = lifespan
|
| 34 |
+
|
| 35 |
+
# Healthcheck endpoint
|
| 36 |
@app.get("/health")
|
| 37 |
async def health():
|
| 38 |
+
return {"status": "ok", "message": "Terabox Bot is running 🚀"}
|
| 39 |
+
|
| 40 |
+
# Optionally index page
|
| 41 |
+
@app.get("/")
|
| 42 |
+
async def index():
|
| 43 |
+
return {
|
| 44 |
+
"bot": "Terabox Downloader Bot",
|
| 45 |
+
"version": "1.0",
|
| 46 |
+
"health": "/health",
|
| 47 |
+
"workers": "4",
|
| 48 |
+
"note": "Send /start to the bot in Telegram to begin."
|
| 49 |
+
}
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|