87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
from fastapi import FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import uvicorn
|
|
from models import DiagramItem
|
|
import json
|
|
import os
|
|
import requests
|
|
import xml.etree.ElementTree as ET
|
|
from typing import List, Dict
|
|
|
|
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"
|
|
DATA_FILE = "diagram.json"
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return {"message": "Check if the server is running"}
|
|
|
|
@app.get("/diagram/fetch")
|
|
def fetch_diagram():
|
|
if not os.path.exists(DATA_FILE):
|
|
return {"nodes": [], "edges": []}
|
|
with open(DATA_FILE, "r") as f:
|
|
return json.load(f)
|
|
|
|
|
|
@app.post("/diagram/save")
|
|
def save_diagram(payload: DiagramItem):
|
|
try:
|
|
with open(DATA_FILE, "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("/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(app, host="0.0.0.0", port=8000)
|
|
|