File size: 1,273 Bytes
3355f04
 
 
 
e9ee9c9
3355f04
e9ee9c9
3355f04
 
 
e9ee9c9
3355f04
 
 
 
e9ee9c9
8c1f5e4
 
 
 
 
 
3355f04
 
 
 
e9ee9c9
3355f04
8c1f5e4
e9ee9c9
3355f04
 
8c1f5e4
3355f04
 
e9ee9c9
3355f04
 
 
 
49a7bcf
3355f04
8c1f5e4
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
41
42
43
import os
import subprocess
from fastapi import FastAPI
import threading

app = FastAPI()

GITHUB_USERNAME = os.getenv("GITHUB_USERNAME")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
REPO_NAME = os.getenv("REPO_NAME")

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 setup_env():
    sample = "repo/.env.example"
    target = "repo/.env"
    if os.path.exists(sample) and not os.path.exists(target):
        subprocess.run(["cp", sample, target], 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(["python3", "-m" , "Hazel"], cwd="repo")

def run_app():
    clone_repo()
    setup_env()  # 👈 added here
    install_requirements()
    start_script()

@app.on_event("startup")
def startup_event():
    threading.Thread(target=run_app).start()

@app.api_route("/", methods=["GET", "HEAD"])
def home():
    return {"status": "Repo cloned, env setup, requirements installed, script running 🚀"}