navix/backend/main.py
2025-06-03 14:38:47 +03:00

93 lines
2.2 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import yaml
from pathlib import Path
from pydantic import BaseModel
from minio import Minio
from datetime import timedelta
app = FastAPI()
# Allow CORS for all origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# MinIO connection with access and secret keys
minio_client = Minio(
"minio.dvirlabs.com",
access_key="YOUR_MINIO_ACCESS_KEY",
secret_key="YOUR_MINIO_SECRET_KEY",
secure=True # Set to False if using HTTP
)
BUCKET = "navix-icons"
@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"}
@app.get("/icon/{filename}")
def get_presigned_icon_url(filename: str):
try:
url = minio_client.presigned_get_object(
bucket_name=BUCKET,
object_name=filename,
expires=timedelta(hours=1)
)
return {"url": url}
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)