navix/backend/main.py

69 lines
1.6 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import yaml
from pathlib import Path
from pydantic import BaseModel
app = FastAPI()
# Allow CORS for all origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Welcome to the FastAPI application!"}
# Path to apps.yaml (relative to backend/)
APPS_FILE = Path(__file__).parent / "apps.yaml"
@app.get("/apps")
def get_apps():
if not APPS_FILE.exists():
return {"error": "apps.yaml not found"}
with open(APPS_FILE, "r") as f:
return yaml.safe_load(f)
class AppData(BaseModel):
name: str
icon: str
description: str
url: str
class AppEntry(BaseModel):
section: str
app: AppData
@app.post("/add_app")
def add_app(entry: AppEntry):
if not APPS_FILE.exists():
current = {"sections": []}
else:
with open(APPS_FILE, "r") as f:
current = yaml.safe_load(f) or {"sections": []}
# Find or create section
for section in current["sections"]:
if section["name"] == entry.section:
section["apps"].append(entry.app.dict())
break
else:
current["sections"].append({
"name": entry.section,
"apps": [entry.app.dict()]
})
with open(APPS_FILE, "w") as f:
yaml.safe_dump(current, f)
return {"status": "added"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)