File size: 1,245 Bytes
b1dffb5
 
 
973f6c5
b1dffb5
973f6c5
 
 
7eee8d3
b1dffb5
fb3fb06
 
 
b1dffb5
fb3fb06
b1dffb5
 
 
 
 
 
973f6c5
7eee8d3
b1dffb5
 
fb3fb06
 
 
b1dffb5
fb3fb06
b1dffb5
 
 
 
 
7eee8d3
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
import os, json

app = FastAPI()
PROJECTS_DIR = "projects"
os.makedirs(PROJECTS_DIR, exist_ok=True)


@app.get("/load")
def load_project(project: str):
    """
    GET /load?project=FILENAME
    Returns the JSON contents of projects/FILENAME
    """
    path = os.path.join(PROJECTS_DIR, os.path.basename(project))
    if not os.path.isfile(path):
        raise HTTPException(status_code=404, detail="Project not found")
    with open(path, "r", encoding="utf-8") as f:
        data = json.load(f)
    return JSONResponse(content=data)


@app.post("/save")
async def save_project(project: str, request: Request):
    """
    POST /save?project=FILENAME
    Body: JSON project data
    Writes the data back into projects/FILENAME
    """
    body = await request.json()
    path = os.path.join(PROJECTS_DIR, os.path.basename(project))
    with open(path, "w", encoding="utf-8") as f:
        json.dump(body, f, indent=2)
    return JSONResponse(content={"success": True})


if __name__ == "__main__":
    # When you run `python app.py`, this will start Uvicorn on the HF Spaces port.
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)