34 lines
884 B
Python
34 lines
884 B
Python
from fastapi import FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from models import DiagramItem
|
|
import json
|
|
import os
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # בהמשך תוכל לצמצם לכתובת הפרונטאנד
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
DATA_FILE = "diagram.json"
|
|
|
|
@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))
|