from fastapi import FastAPI from fastapi import FastAPI, APIRouter # Added APIRouter from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse import os from backend.core.config import settings # Renamed to settings_config to avoid conflict from backend.api.endpoints import playlists, search, stream, lyrics, settings as settings_router # Aliased settings router app = FastAPI(title=settings.APP_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json") # Used settings_config # CORS setup app.add_middleware( CORSMiddleware, allow_origins=settings.BACKEND_CORS_ORIGINS, # Used settings_config allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Include Routers api_router = APIRouter() api_router.include_router(playlists.router, prefix="/playlists", tags=["playlists"]) api_router.include_router(search.router, tags=["search"]) api_router.include_router(stream.router, tags=["stream"]) api_router.include_router(lyrics.router, tags=["lyrics"]) api_router.include_router(settings_router.router, prefix="/settings", tags=["settings"]) # Included settings_router app.include_router(api_router, prefix=settings.API_V1_STR) # Corrected prefix and removed extra tags # 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"}