57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
from pathlib import Path
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import Response
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from backend.audio_export import export_keyboard_audio
|
|
from backend.models import ExportRequest
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
DIST = ROOT / "frontend" / "dist"
|
|
ASSETS = ROOT / "assets"
|
|
|
|
app = FastAPI(title="sfxkeeb")
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
if ASSETS.exists():
|
|
app.mount("/assets", StaticFiles(directory=ASSETS), name="assets")
|
|
|
|
|
|
@app.post("/api/export/audio")
|
|
def export_audio(request: ExportRequest) -> Response:
|
|
try:
|
|
wav_bytes = export_keyboard_audio(request)
|
|
except FileNotFoundError as exc:
|
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
|
except Exception as exc:
|
|
raise HTTPException(status_code=500, detail=f"Export failed: {exc}") from exc
|
|
|
|
return Response(
|
|
content=wav_bytes,
|
|
media_type="audio/wav",
|
|
headers={"Content-Disposition": 'attachment; filename="keyboard_track.wav"'},
|
|
)
|
|
|
|
|
|
@app.get("/api/health")
|
|
def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|
|
|
|
|
|
if DIST.exists():
|
|
app.mount("/", StaticFiles(directory=DIST, html=True), name="frontend")
|
|
|
|
|
|
def run() -> None:
|
|
uvicorn.run("backend.main:app", host="127.0.0.1", port=8000, reload=True)
|