AudioForge / backend /app_hf.py
OnyxlMunkey's picture
Initial commit (clean)
6423ff2
"""
Hugging Face Space Entrypoint.
Serves the API and the Static Frontend.
"""
import os
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from app.main import app
# Ensure we use SQLite for Spaces if not set
if "DATABASE_URL" not in os.environ:
os.environ["DATABASE_URL"] = "sqlite+aiosqlite:///./storage/audioforge.db"
# Mount Static Frontend
# We assume the frontend is built to /app/static
if os.path.exists("/app/static"):
app.mount("/_next", StaticFiles(directory="/app/static/_next"), name="static_next")
app.mount("/assets", StaticFiles(directory="/app/static/assets"), name="static_assets")
# Serve icon
@app.get("/icon")
async def icon():
return FileResponse("/app/static/icon")
# SPA Catch-all (for routes not matching API or static files)
@app.get("/{full_path:path}")
async def serve_spa(full_path: str):
if full_path.startswith("api"):
from fastapi import HTTPException
raise HTTPException(status_code=404, detail="Not Found")
# Check if file exists in static (e.g. favicon.ico)
possible_path = f"/app/static/{full_path}"
if os.path.isfile(possible_path):
return FileResponse(possible_path)
return FileResponse("/app/static/index.html")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)