dragonxd1 commited on
Commit
687d463
·
verified ·
1 Parent(s): bfde61f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -65
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import os
2
  import subprocess
3
  import threading
4
- from fastapi import FastAPI, Request
5
- from fastapi.responses import JSONResponse, HTMLResponse
6
- import httpx
7
 
8
  app = FastAPI(docs_url=None, redoc_url=None)
9
 
@@ -12,7 +10,7 @@ GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "").strip()
12
  GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "").strip()
13
  REPO_NAME = os.getenv("REPO_NAME", "").strip()
14
 
15
- # Your userbot's internal port
16
  USERBOT_API_PORT = 8888
17
 
18
  def clone_repo():
@@ -35,64 +33,4 @@ def run_app():
35
 
36
  @app.on_event("startup")
37
  def startup_event():
38
- threading.Thread(target=run_app).start()
39
-
40
- @app.get("/")
41
- def home():
42
- return {"status": "Repo cloned, requirements installed, start script running 🚀"}
43
-
44
- # --- MAGIC PROXY SYSTEM ---
45
-
46
- @app.get("/status")
47
- async def proxy_status():
48
- try:
49
- async with httpx.AsyncClient() as client:
50
- response = await client.get(f"http://127.0.0.1:{USERBOT_API_PORT}/status")
51
- return response.json()
52
- except Exception:
53
- return {"error": "Userbot API is strictly booting up. Give it a second!"}
54
-
55
- # Forward URL/docs Swagger UI from Userbot
56
- @app.get("/docs")
57
- async def proxy_docs(request: Request):
58
- headers = dict(request.headers)
59
- headers.pop("host", None)
60
- try:
61
- async with httpx.AsyncClient() as client:
62
- response = await client.get(f"http://127.0.0.1:{USERBOT_API_PORT}/docs", headers=headers)
63
- return HTMLResponse(status_code=response.status_code, content=response.content)
64
- except Exception:
65
- return HTMLResponse(status_code=503, content="<h1>Userbot API is booting up... Refresh in 5 seconds!</h1>")
66
-
67
- # Forward OpenAPI schema (Swagger UI needs this)
68
- @app.get("/openapi.json")
69
- async def proxy_openapi(request: Request):
70
- headers = dict(request.headers)
71
- headers.pop("host", None)
72
- try:
73
- async with httpx.AsyncClient() as client:
74
- response = await client.get(f"http://127.0.0.1:{USERBOT_API_PORT}/openapi.json", headers=headers)
75
- return response.json()
76
- except Exception:
77
- return {"error": "Userbot API is booting up..."}
78
-
79
- # Forward all /api/ traffic dynamically
80
- @app.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
81
- async def proxy_to_userbot(request: Request, path: str):
82
- url = f"http://127.0.0.1:{USERBOT_API_PORT}/api/{path}"
83
- headers = dict(request.headers)
84
- headers.pop("host", None)
85
-
86
- try:
87
- async with httpx.AsyncClient() as client:
88
- route_method = getattr(client, request.method.lower())
89
- body = await request.body()
90
- response = await route_method(
91
- url, headers=headers, content=body
92
- )
93
- return JSONResponse(status_code=response.status_code, content=response.json())
94
- except Exception as e:
95
- return JSONResponse(
96
- status_code=502,
97
- content={"error": f"Userbot API is offline or booting up. Exception: {str(e)}"},
98
- )
 
1
  import os
2
  import subprocess
3
  import threading
4
+ from fastapi import FastAPI
 
 
5
 
6
  app = FastAPI(docs_url=None, redoc_url=None)
7
 
 
10
  GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "").strip()
11
  REPO_NAME = os.getenv("REPO_NAME", "").strip()
12
 
13
+ # Your userbot's internal port (still here if you need it later)
14
  USERBOT_API_PORT = 8888
15
 
16
  def clone_repo():
 
33
 
34
  @app.on_event("startup")
35
  def startup_event():
36
+ threading.Thread(target=run_app).start()