File size: 1,205 Bytes
3355f04
 
 
687d463
e9ee9c9
5497b54
e9ee9c9
5497b54
 
 
 
0d860ca
687d463
5b2b140
e9ee9c9
3355f04
 
5497b54
3355f04
e9ee9c9
3355f04
95de6c2
 
5b2b140
e9ee9c9
3355f04
5b2b140
e9ee9c9
3355f04
5b2b140
 
 
e9ee9c9
180e486
0cebeb1
 
 
3355f04
 
687d463
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import subprocess
import threading
from fastapi import FastAPI

app = FastAPI(docs_url=None, redoc_url=None)

# Use .strip() to remove any accidental newlines or spaces from HF Secrets!
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "").strip()
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "").strip()
REPO_NAME = os.getenv("REPO_NAME", "").strip()

# Your userbot's internal port (still here if you need it later)
USERBOT_API_PORT = 8888  

def clone_repo():
    if not os.path.exists("repo"):
        repo_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
        subprocess.run(["git", "clone", repo_url, "repo"], check=True)

def install_requirements():
    req_file = "repo/requirements.txt"
    if os.path.exists(req_file):
        subprocess.run(["pip", "install", "--no-cache-dir", "-r", req_file], check=True)

def start_script():
    subprocess.Popen(["bash", "startup"], cwd="repo")

def run_app():
    clone_repo()
    install_requirements()
    start_script()

@app.api_route("/", methods=["GET", "HEAD"])
def root():
    return {"status": "ok"}
    
@app.on_event("startup")
def startup_event():
    threading.Thread(target=run_app).start()