Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# filename: main.py
|
| 2 |
|
| 3 |
import logging
|
| 4 |
import asyncio
|
|
@@ -11,6 +11,7 @@ load_dotenv()
|
|
| 11 |
from fastapi import FastAPI
|
| 12 |
from fastapi.responses import FileResponse, PlainTextResponse
|
| 13 |
import uvicorn
|
|
|
|
| 14 |
|
| 15 |
# Now, import our application modules AFTER loading the environment
|
| 16 |
import config
|
|
@@ -21,15 +22,13 @@ from database import manager as db_manager
|
|
| 21 |
from handlers import start, admin, user, links
|
| 22 |
|
| 23 |
# --- Logging Configuration ---
|
| 24 |
-
# Creates the logs directory if it doesn't exist
|
| 25 |
os.makedirs("logs", exist_ok=True)
|
| 26 |
-
|
| 27 |
logging.basicConfig(
|
| 28 |
level=logging.INFO,
|
| 29 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 30 |
handlers=[
|
| 31 |
-
logging.StreamHandler(),
|
| 32 |
-
logging.FileHandler("logs/bot.log", mode='a', encoding='utf-8')
|
| 33 |
]
|
| 34 |
)
|
| 35 |
logger = logging.getLogger(__name__)
|
|
@@ -55,6 +54,17 @@ async def startup_event():
|
|
| 55 |
# 1. Initialize the database and create indexes for fast queries
|
| 56 |
await db_manager.setup_database_indexes()
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# 2. Start the Telethon client
|
| 59 |
logger.info("Starting Telegram client...")
|
| 60 |
await bot.start(bot_token=config.BOT_TOKEN)
|
|
@@ -79,12 +89,9 @@ async def shutdown_event():
|
|
| 79 |
|
| 80 |
|
| 81 |
# --- FastAPI Routes (Web Endpoints) ---
|
|
|
|
| 82 |
@app.get("/", response_class=FileResponse)
|
| 83 |
async def read_index():
|
| 84 |
-
"""
|
| 85 |
-
Serves the main web interface if index.html exists.
|
| 86 |
-
Otherwise, returns a simple status message.
|
| 87 |
-
"""
|
| 88 |
index_path = "index.html"
|
| 89 |
if os.path.exists(index_path):
|
| 90 |
return FileResponse(index_path)
|
|
@@ -93,7 +100,6 @@ async def read_index():
|
|
| 93 |
|
| 94 |
@app.get("/health", response_class=PlainTextResponse)
|
| 95 |
async def health_check():
|
| 96 |
-
"""A simple endpoint for health checks and monitoring."""
|
| 97 |
if bot.is_connected():
|
| 98 |
return PlainTextResponse("✅ OK: Bot service is running and connected to Telegram.")
|
| 99 |
else:
|
|
@@ -103,11 +109,9 @@ async def health_check():
|
|
| 103 |
# --- Main Execution Block ---
|
| 104 |
if __name__ == "__main__":
|
| 105 |
logger.info("Starting Uvicorn server...")
|
| 106 |
-
# Uvicorn runs the FastAPI application.
|
| 107 |
-
# 'reload=True' is for development; you might remove it in production.
|
| 108 |
uvicorn.run(
|
| 109 |
"main:app",
|
| 110 |
host="0.0.0.0",
|
| 111 |
-
port=8080,
|
| 112 |
-
reload=True
|
| 113 |
)
|
|
|
|
| 1 |
+
# filename: main.py (Updated with DNS Resolver in Startup)
|
| 2 |
|
| 3 |
import logging
|
| 4 |
import asyncio
|
|
|
|
| 11 |
from fastapi import FastAPI
|
| 12 |
from fastapi.responses import FileResponse, PlainTextResponse
|
| 13 |
import uvicorn
|
| 14 |
+
import dns.asyncresolver # <-- NEW IMPORT
|
| 15 |
|
| 16 |
# Now, import our application modules AFTER loading the environment
|
| 17 |
import config
|
|
|
|
| 22 |
from handlers import start, admin, user, links
|
| 23 |
|
| 24 |
# --- Logging Configuration ---
|
|
|
|
| 25 |
os.makedirs("logs", exist_ok=True)
|
|
|
|
| 26 |
logging.basicConfig(
|
| 27 |
level=logging.INFO,
|
| 28 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 29 |
handlers=[
|
| 30 |
+
logging.StreamHandler(),
|
| 31 |
+
logging.FileHandler("logs/bot.log", mode='a', encoding='utf-8')
|
| 32 |
]
|
| 33 |
)
|
| 34 |
logger = logging.getLogger(__name__)
|
|
|
|
| 54 |
# 1. Initialize the database and create indexes for fast queries
|
| 55 |
await db_manager.setup_database_indexes()
|
| 56 |
|
| 57 |
+
# --- DNS RESOLVER CODE ADDED HERE, AS REQUESTED ---
|
| 58 |
+
try:
|
| 59 |
+
logger.info("Attempting to configure custom DNS resolvers...")
|
| 60 |
+
async_resolver = dns.asyncresolver.Resolver(configure=False)
|
| 61 |
+
async_resolver.nameservers = ["8.8.8.8", "8.8.4.4", "1.1.1.1"]
|
| 62 |
+
dns.asyncresolver.default_resolver = async_resolver
|
| 63 |
+
logger.info("Successfully configured custom DNS resolvers [Google, Cloudflare].")
|
| 64 |
+
except Exception as e:
|
| 65 |
+
logger.warning(f"Could not configure custom DNS resolver: {e}")
|
| 66 |
+
# --- END OF DNS CODE ---
|
| 67 |
+
|
| 68 |
# 2. Start the Telethon client
|
| 69 |
logger.info("Starting Telegram client...")
|
| 70 |
await bot.start(bot_token=config.BOT_TOKEN)
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
# --- FastAPI Routes (Web Endpoints) ---
|
| 92 |
+
# ... (The rest of the file is unchanged) ...
|
| 93 |
@app.get("/", response_class=FileResponse)
|
| 94 |
async def read_index():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
index_path = "index.html"
|
| 96 |
if os.path.exists(index_path):
|
| 97 |
return FileResponse(index_path)
|
|
|
|
| 100 |
|
| 101 |
@app.get("/health", response_class=PlainTextResponse)
|
| 102 |
async def health_check():
|
|
|
|
| 103 |
if bot.is_connected():
|
| 104 |
return PlainTextResponse("✅ OK: Bot service is running and connected to Telegram.")
|
| 105 |
else:
|
|
|
|
| 109 |
# --- Main Execution Block ---
|
| 110 |
if __name__ == "__main__":
|
| 111 |
logger.info("Starting Uvicorn server...")
|
|
|
|
|
|
|
| 112 |
uvicorn.run(
|
| 113 |
"main:app",
|
| 114 |
host="0.0.0.0",
|
| 115 |
+
port=8080,
|
| 116 |
+
reload=True
|
| 117 |
)
|