Spaces:
Paused
Paused
CBV01 commited on
Commit ·
8a49d3e
1
Parent(s): fe0cd73
Fix telethon requirement 404, optimize db startup, and support dynamic port for Render
Browse files- Dockerfile +1 -2
- backend/database.py +10 -0
- backend/main.py +5 -4
Dockerfile
CHANGED
|
@@ -18,5 +18,4 @@ COPY --chown=user backend/ .
|
|
| 18 |
ENV PORT=7860
|
| 19 |
EXPOSE 7860
|
| 20 |
|
| 21 |
-
|
| 22 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--proxy-headers", "--forwarded-allow-ips", "*"]
|
|
|
|
| 18 |
ENV PORT=7860
|
| 19 |
EXPOSE 7860
|
| 20 |
|
| 21 |
+
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860} --proxy-headers --forwarded-allow-ips '*'"]
|
|
|
backend/database.py
CHANGED
|
@@ -333,6 +333,16 @@ def init_db():
|
|
| 333 |
]
|
| 334 |
|
| 335 |
conn = cast(Any, get_db_connection())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
for q in table_queries:
|
| 337 |
conn.execute(q)
|
| 338 |
|
|
|
|
| 333 |
]
|
| 334 |
|
| 335 |
conn = cast(Any, get_db_connection())
|
| 336 |
+
# Fast path: check if database schema and seed data already exist to avoid 50+ sequential network round-trips
|
| 337 |
+
try:
|
| 338 |
+
plans_check = conn.execute("SELECT COUNT(*) FROM plans").fetchone()
|
| 339 |
+
if plans_check and plans_check[0] >= 5:
|
| 340 |
+
print("Database already initialized and up to date.")
|
| 341 |
+
if hasattr(conn, "close"): conn.close()
|
| 342 |
+
return
|
| 343 |
+
except Exception:
|
| 344 |
+
pass
|
| 345 |
+
|
| 346 |
for q in table_queries:
|
| 347 |
conn.execute(q)
|
| 348 |
|
backend/main.py
CHANGED
|
@@ -53,10 +53,6 @@ def log_debug(msg: str):
|
|
| 53 |
print(formatted)
|
| 54 |
LOG_BUFFER.append(formatted)
|
| 55 |
|
| 56 |
-
@app.get("/api/debug/logs")
|
| 57 |
-
async def get_logs(user_id: str = Depends(get_current_admin)):
|
| 58 |
-
return {"logs": list(LOG_BUFFER)}
|
| 59 |
-
|
| 60 |
# --- Tiered Plan Configurations (Dynamic from DB) ---
|
| 61 |
@functools.lru_cache(maxsize=1)
|
| 62 |
def get_plan_configs() -> Dict[str, Dict[str, Any]]:
|
|
@@ -253,6 +249,11 @@ if not os.path.exists("static/avatars"):
|
|
| 253 |
os.makedirs("static/avatars", exist_ok=True)
|
| 254 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
async def fetch_tg_profile_data(client: TelegramClient, phone_number: str):
|
| 257 |
"""Fetches profile details and downloads photo for an authenticated client."""
|
| 258 |
me = await client.get_me()
|
|
|
|
| 53 |
print(formatted)
|
| 54 |
LOG_BUFFER.append(formatted)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# --- Tiered Plan Configurations (Dynamic from DB) ---
|
| 57 |
@functools.lru_cache(maxsize=1)
|
| 58 |
def get_plan_configs() -> Dict[str, Dict[str, Any]]:
|
|
|
|
| 249 |
os.makedirs("static/avatars", exist_ok=True)
|
| 250 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 251 |
|
| 252 |
+
# Moved here so 'app' is defined before this route is registered (was crashing import).
|
| 253 |
+
@app.get("/api/debug/logs")
|
| 254 |
+
async def get_logs(user_id: str = Depends(get_current_admin)):
|
| 255 |
+
return {"logs": list(LOG_BUFFER)}
|
| 256 |
+
|
| 257 |
async def fetch_tg_profile_data(client: TelegramClient, phone_number: str):
|
| 258 |
"""Fetches profile details and downloads photo for an authenticated client."""
|
| 259 |
me = await client.get_me()
|