from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse import os from backend.core.config import settings from backend.api.endpoints import playlists, search, stream, lyrics app = FastAPI(title=settings.APP_NAME) # CORS setup app.add_middleware( CORSMiddleware, allow_origins=settings.BACKEND_CORS_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include Routers app.include_router(playlists.router, prefix=f"{settings.API_V1_STR}", tags=["playlists"]) app.include_router(search.router, prefix=f"{settings.API_V1_STR}", tags=["search"]) app.include_router(stream.router, prefix=f"{settings.API_V1_STR}", tags=["stream"]) app.include_router(lyrics.router, prefix=f"{settings.API_V1_STR}", tags=["lyrics"]) # Serve Static Frontend (Production Mode) if settings.CACHE_DIR.parent.name == "backend": # assuming running from root STATIC_DIR = "static" else: STATIC_DIR = "../static" if os.path.exists(STATIC_DIR): app.mount("/_next", StaticFiles(directory=os.path.join(STATIC_DIR, "_next")), name="next_assets") @app.get("/{full_path:path}") async def serve_spa(full_path: str): file_path = os.path.join(STATIC_DIR, full_path) if os.path.isfile(file_path): return FileResponse(file_path) index_path = os.path.join(STATIC_DIR, "index.html") if os.path.exists(index_path): return FileResponse(index_path) return {"error": "Frontend not found"} else: @app.get("/") def read_root(): return {"message": "Spotify Clone Backend Running (Frontend not built/mounted)"} @app.get("/health") def health_check(): return {"status": "ok"}