Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,36 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
import threading
|
| 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 |
+
repo_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_USERNAME}/{REPO_NAME}.git"
|
| 15 |
+
subprocess.run(["git", "clone", repo_url, "repo"], check=True)
|
| 16 |
|
| 17 |
+
def install_requirements():
|
| 18 |
+
req_file = "repo/requirements.txt"
|
| 19 |
+
if os.path.exists(req_file):
|
| 20 |
+
subprocess.run(["pip", "install", "--no-cache-dir", "-r", req_file], check=True)
|
| 21 |
|
| 22 |
+
def start_script():
|
| 23 |
+
subprocess.Popen(["bash", "start"], cwd="repo")
|
| 24 |
|
| 25 |
+
def run_app():
|
| 26 |
+
clone_repo()
|
| 27 |
+
install_requirements()
|
| 28 |
+
start_script()
|
| 29 |
|
| 30 |
+
@app.on_event("startup")
|
| 31 |
+
def startup_event():
|
| 32 |
+
threading.Thread(target=run_app).start()
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def home():
|
| 36 |
+
return {"status": "Repo cloned, requirements installed, start script running 🚀"}
|