75 lines
1.9 KiB
Python
75 lines
1.9 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 AppEntry(BaseModel):
|
|
section: str
|
|
name: str
|
|
icon: str
|
|
description: str
|
|
url: str
|
|
|
|
@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({
|
|
"name": entry.name,
|
|
"icon": entry.icon,
|
|
"description": entry.description,
|
|
"url": entry.url,
|
|
})
|
|
break
|
|
else:
|
|
current["sections"].append({
|
|
"name": entry.section,
|
|
"apps": [{
|
|
"name": entry.name,
|
|
"icon": entry.icon,
|
|
"description": entry.description,
|
|
"url": entry.url,
|
|
}]
|
|
})
|
|
|
|
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) |