Spaces:
Running
Running
Update app.py
Browse files
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"
|
| 9 |
-
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN"
|
| 10 |
-
REPO_NAME = os.getenv("REPO_NAME"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def clone_repo():
|
| 13 |
if not os.path.exists("repo"):
|
| 14 |
-
|
| 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 |
-
|
| 24 |
-
if os.path.exists(
|
| 25 |
-
|
| 26 |
-
subprocess.run(["pip", "install", "--no-cache-dir", "-r", req], check=True)
|
| 27 |
|
| 28 |
def start_script():
|
| 29 |
-
|
| 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": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"}
|