-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevbox_api.py
More file actions
56 lines (51 loc) · 1.91 KB
/
devbox_api.py
File metadata and controls
56 lines (51 loc) · 1.91 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
from fastapi import FastAPI
import sqlite3
import subprocess
from pydantic import BaseModel
app = FastAPI()
DB_PATH = "../devbox/devbox.db"
class Artifact(BaseModel):
user: str
language: str
type: str
name: str
content: str
@app.post("/devbox/create")
def create_artifact(a: Artifact):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute(
"INSERT INTO artifacts (user_id, language, type, name, content, created) VALUES (?, ?, ?, ?, ?, datetime('now'))",
(a.user, a.language, a.type, a.name, a.content),
)
conn.commit()
conn.close()
return {"status": "ok"}
@app.get("/devbox/artifact/{artifact_id}")
def get_artifact(artifact_id: int):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("SELECT content FROM artifacts WHERE id = ?", (artifact_id,))
row = c.fetchone()
conn.close()
return {"content": row[0] if row else ""}
@app.post("/devbox/run")
def run_artifact(a: Artifact):
# Run in isolated subprocess depending on language
output = ""
try:
if a.language.lower() == "python":
result = subprocess.run(["python3", "-c", a.content], capture_output=True, text=True, timeout=10)
output = result.stdout + result.stderr
elif a.language.lower() == "r":
result = subprocess.run(["Rscript", "-e", a.content], capture_output=True, text=True, timeout=10)
output = result.stdout + result.stderr
elif a.language.lower() == "julia":
result = subprocess.run(["julia", "-e", a.content], capture_output=True, text=True, timeout=10)
output = result.stdout + result.stderr
# Add additional languages here: Kotlin, Java, Swift, Go, JS, HTML build logic
else:
output = f"Execution for {a.language} not implemented yet."
except subprocess.TimeoutExpired:
output = "Execution timed out!"
return {"output": output}