dragxd commited on
Commit
64fd368
·
verified ·
1 Parent(s): 3550fa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, Request, HTTPException, UploadFile, File, Form
2
  from pydantic import BaseModel
3
  import uuid
4
  from fastapi.responses import RedirectResponse
@@ -7,6 +7,7 @@ import os
7
  import requests
8
  from urllib.parse import urlencode
9
  import aiofiles
 
10
 
11
  app = FastAPI()
12
  deployments = {}
@@ -14,7 +15,7 @@ deployments = {}
14
  # GitHub OAuth config (replace with your own client_id and client_secret)
15
  GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID", "your_github_client_id")
16
  GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET", "your_github_client_secret")
17
- GITHUB_OAUTH_REDIRECT = os.getenv("GITHUB_OAUTH_REDIRECT", "https://dragxd-host.hf.space/github/callback")
18
 
19
  # In-memory user token storage (for demo; use DB in production)
20
  user_tokens = {}
@@ -25,6 +26,11 @@ user_selected_repo = {}
25
  # In-memory .env storage: {(user_id, repo_full_name): env_content}
26
  env_files = {}
27
 
 
 
 
 
 
28
  app.add_middleware(
29
  CORSMiddleware,
30
  allow_origins=["*"],
@@ -41,19 +47,32 @@ class RepoSelectRequest(BaseModel):
41
  user_id: str
42
  repo_full_name: str
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  @app.post("/deploy")
45
- async def deploy(req: DeployRequest):
46
- # Simulate deployment
 
 
 
47
  deploy_id = str(uuid.uuid4())
48
- deployments[deploy_id] = {
49
- "github_url": req.github_url,
50
- "user_id": req.user_id,
51
- "status": "deploying",
52
- "url": None
53
- }
54
- # Simulate deployment done after a few seconds (in real app, use background task)
55
- deployments[deploy_id]["status"] = "success"
56
- deployments[deploy_id]["url"] = f"https://demo-deployment/{deploy_id}"
57
  return {"deploy_id": deploy_id}
58
 
59
  @app.get("/status/{deploy_id}")
@@ -119,4 +138,15 @@ async def upload_env(user_id: str = Form(...), file: UploadFile = File(...)):
119
  if not repo_full_name:
120
  return {"error": "No repo selected for user."}
121
  env_files[(user_id, repo_full_name)] = content.decode()
122
- return {"message": ".env uploaded", "repo_full_name": repo_full_name}
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException, UploadFile, File, Form, BackgroundTasks
2
  from pydantic import BaseModel
3
  import uuid
4
  from fastapi.responses import RedirectResponse
 
7
  import requests
8
  from urllib.parse import urlencode
9
  import aiofiles
10
+ import asyncio
11
 
12
  app = FastAPI()
13
  deployments = {}
 
15
  # GitHub OAuth config (replace with your own client_id and client_secret)
16
  GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID", "your_github_client_id")
17
  GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET", "your_github_client_secret")
18
+ GITHUB_OAUTH_REDIRECT = os.getenv("GITHUB_OAUTH_REDIRECT", "http://localhost:7860/github/callback")
19
 
20
  # In-memory user token storage (for demo; use DB in production)
21
  user_tokens = {}
 
26
  # In-memory .env storage: {(user_id, repo_full_name): env_content}
27
  env_files = {}
28
 
29
+ # In-memory log storage: {deploy_id: [log lines]}
30
+ deploy_logs = {}
31
+ # In-memory deploy status: {deploy_id: "deploying"|"success"|"failed"}
32
+ deploy_status = {}
33
+
34
  app.add_middleware(
35
  CORSMiddleware,
36
  allow_origins=["*"],
 
47
  user_id: str
48
  repo_full_name: str
49
 
50
+ async def simulate_deployment(deploy_id, user_id, repo_full_name):
51
+ logs = [
52
+ f"Starting deployment for {repo_full_name}...",
53
+ "Cloning repository...",
54
+ "Installing dependencies...",
55
+ "Setting environment variables...",
56
+ "Running build...",
57
+ "Deploying application...",
58
+ "Finalizing...",
59
+ "Deployment successful!"
60
+ ]
61
+ deploy_logs[deploy_id] = []
62
+ deploy_status[deploy_id] = "deploying"
63
+ for log in logs:
64
+ deploy_logs[deploy_id].append(log)
65
+ await asyncio.sleep(2) # Simulate time between log lines
66
+ deploy_status[deploy_id] = "success"
67
+
68
  @app.post("/deploy")
69
+ async def deploy(req: DeployRequest, background_tasks: BackgroundTasks):
70
+ user_id = req.user_id
71
+ repo_full_name = user_selected_repo.get(user_id)
72
+ if not repo_full_name:
73
+ return {"error": "No repo selected for user."}
74
  deploy_id = str(uuid.uuid4())
75
+ background_tasks.add_task(simulate_deployment, deploy_id, user_id, repo_full_name)
 
 
 
 
 
 
 
 
76
  return {"deploy_id": deploy_id}
77
 
78
  @app.get("/status/{deploy_id}")
 
138
  if not repo_full_name:
139
  return {"error": "No repo selected for user."}
140
  env_files[(user_id, repo_full_name)] = content.decode()
141
+ return {"message": ".env uploaded", "repo_full_name": repo_full_name}
142
+
143
+ @app.get("/logs/{deploy_id}")
144
+ def get_logs(deploy_id: str, last_line: int = 0):
145
+ logs = deploy_logs.get(deploy_id, [])
146
+ status = deploy_status.get(deploy_id, "unknown")
147
+ # Return new log lines since last_line
148
+ return {
149
+ "logs": logs[last_line:],
150
+ "next_line": len(logs),
151
+ "status": status
152
+ }