labmap/backend/main.py
2025-07-11 18:18:08 +03:00

107 lines
3.1 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=["*"],
)
# Static icon info
BASE_URL = "https://s3.dvirlabs.com/lab-icons"
S3_INDEX_URL = "https://s3.dvirlabs.com/lab-icons/?list-type=2"
# Directory for storing diagrams
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):
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():
"""
Returns a dictionary of available icons grouped by folder (category).
Example:
{
"dev-tools": [ "https://s3.dvirlabs.com/lab-icons/dev-tools/gitea.svg", ... ],
"observability": [ ... ]
}
"""
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)