labmap/backend/main.py

99 lines
2.9 KiB
Python

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from models import DiagramItem
import json
import requests
import xml.etree.ElementTree as ET
from typing import List, Dict
from pathlib import Path
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
BASE_URL = "https://s3.dvirlabs.com/lab-icons"
S3_INDEX_URL = "https://s3.dvirlabs.com/lab-icons/?list-type=2"
BASE_DIR = Path(__file__).parent.resolve()
DATA_DIR = BASE_DIR / "diagrams"
DATA_DIR.mkdir(exist_ok=True)
@app.get("/")
def root():
return {"message": "Check if the server is running"}
@app.get("/diagram/fetch")
def fetch_diagram(name: str):
path = DATA_DIR / f"diagram_{name}.json"
if not path.exists():
return {"nodes": [], "edges": []}
with open(path, "r") as f:
return json.load(f)
@app.post("/diagram/save")
def save_diagram(name: str, payload: DiagramItem):
if not name:
raise HTTPException(status_code=400, detail="Missing diagram name")
try:
path = DATA_DIR / f"diagram_{name}.json"
with open(path, "w") as f:
json.dump(payload.dict(), f, indent=2)
return {"status": "ok"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/diagram/list")
def list_diagrams():
diagrams = []
for file in DATA_DIR.glob("diagram_*.json"):
diagrams.append(file.stem.replace("diagram_", ""))
return {"diagrams": diagrams}
@app.delete("/diagram/delete")
def delete_diagram(name: str):
path = DATA_DIR / f"diagram_{name}.json"
if not path.exists():
raise HTTPException(status_code=404, detail="Diagram not found")
try:
path.unlink()
return {"status": "deleted"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/icons", response_model=Dict[str, List[str]])
def list_icons():
resp = requests.get(S3_INDEX_URL)
if resp.status_code != 200:
raise HTTPException(status_code=500, detail="Failed to fetch icon list from S3")
root = ET.fromstring(resp.content)
categories: Dict[str, List[str]] = {}
for content in root.findall(".//{http://s3.amazonaws.com/doc/2006-03-01/}Contents"):
key = content.find("{http://s3.amazonaws.com/doc/2006-03-01/}Key").text
if not key.endswith(".svg"):
continue
parts = key.split('/')
if len(parts) == 2:
category, icon = parts
elif len(parts) > 2:
category = parts[0]
icon = parts[-1]
else:
category = "uncategorized"
icon = parts[0]
url = f"{BASE_URL}/{key}"
categories.setdefault(category, []).append(url)
return categories
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)