dragonxd1 commited on
Commit
95de6c2
·
verified ·
1 Parent(s): e11e959

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -1,36 +1,30 @@
1
  import os
2
  import subprocess
3
  import threading
4
- from fastapi import FastAPI
 
 
5
 
6
  app = FastAPI()
7
 
8
- GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "").strip()
9
- GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "").strip()
10
- REPO_NAME = os.getenv("REPO_NAME", "").strip()
 
 
11
 
12
  def clone_repo():
13
  if not os.path.exists("repo"):
14
- if GITHUB_TOKEN:
15
- repo_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
16
- else:
17
- repo_url = f"https://github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
18
-
19
- print(f"Cloning {GITHUB_USERNAME}/{REPO_NAME}...")
20
  subprocess.run(["git", "clone", repo_url, "repo"], check=True)
21
 
22
  def install_requirements():
23
- req = "repo/requirements.txt"
24
- if os.path.exists(req):
25
- print("Installing requirements...")
26
- subprocess.run(["pip", "install", "--no-cache-dir", "-r", req], check=True)
27
 
28
  def start_script():
29
- if os.path.exists("repo/startup"):
30
- print("Starting startup script...")
31
- subprocess.Popen(["bash", "startup"], cwd="repo")
32
- else:
33
- print("startup script not found!")
34
 
35
  def run_app():
36
  clone_repo()
@@ -43,4 +37,36 @@ def startup_event():
43
 
44
  @app.get("/")
45
  def home():
46
- return {"status": "repo running via startup script"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import subprocess
3
  import threading
4
+ from fastapi import FastAPI, Request
5
+ from fastapi.responses import JSONResponse
6
+ import httpx # pip install httpx
7
 
8
  app = FastAPI()
9
 
10
+ GITHUB_USERNAME = os.getenv("GITHUB_USERNAME")
11
+ GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
12
+ REPO_NAME = os.getenv("REPO_NAME")
13
+ # Your userbot's internal port
14
+ USERBOT_API_PORT = 8888
15
 
16
  def clone_repo():
17
  if not os.path.exists("repo"):
18
+ repo_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
 
 
 
 
 
19
  subprocess.run(["git", "clone", repo_url, "repo"], check=True)
20
 
21
  def install_requirements():
22
+ req_file = "repo/requirements.txt"
23
+ if os.path.exists(req_file):
24
+ subprocess.run(["pip", "install", "--no-cache-dir", "-r", req_file], check=True)
 
25
 
26
  def start_script():
27
+ subprocess.Popen(["bash", "startup"], cwd="repo")
 
 
 
 
28
 
29
  def run_app():
30
  clone_repo()
 
37
 
38
  @app.get("/")
39
  def home():
40
+ return {"status": "Repo cloned, requirements installed, start script running 🚀"}
41
+
42
+ # --- MAGIC PROXY ---
43
+ # Forward all traffic starting with /api/ directly down to the Userbot on port 8888!
44
+ @app.route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
45
+ async def proxy_to_userbot(request: Request, path: str):
46
+ url = f"http://127.0.0.1:{USERBOT_API_PORT}/api/{path}"
47
+
48
+ # Extract headers and API Key
49
+ headers = dict(request.headers)
50
+
51
+ # Strip the host header so it doesn't conflict
52
+ headers.pop("host", None)
53
+
54
+ try:
55
+ async with httpx.AsyncClient() as client:
56
+ route_method = getattr(client, request.method.lower())
57
+ body = await request.body()
58
+ response = await route_method(
59
+ url, headers=headers, content=body
60
+ )
61
+ return JSONResponse(status_code=response.status_code, content=response.json())
62
+ except Exception as e:
63
+ return JSONResponse(status_code=502, content={"error": f"Userbot API is offline or booting up. Exception: {str(e)}"})
64
+
65
+ @app.get("/status")
66
+ async def proxy_status():
67
+ try:
68
+ async with httpx.AsyncClient() as client:
69
+ response = await client.get(f"http://127.0.0.1:{USERBOT_API_PORT}/status")
70
+ return response.json()
71
+ except Exception:
72
+ return {"error": "Userbot API is off"}