Spaces:
Paused
Paused
| import os | |
| import uvicorn | |
| from fastapi import FastAPI | |
| from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse, FileResponse | |
| from DragMusic.core import nowplaying | |
| app = FastAPI(docs_url=None, redoc_url="/") | |
| def hello(): | |
| return {"message": "running"} | |
| def get_audio_stream(file_path): | |
| def iterfile(): | |
| with open(file_path, mode="rb") as file_like: | |
| yield from file_like | |
| return iterfile | |
| def mini_web_player(): | |
| html_path = os.path.join(os.path.dirname(__file__), "DragMusic", "web", "player.html") | |
| if not os.path.exists(html_path): | |
| # fallback to old inline HTML if file missing | |
| return "<h2>Mini Web Player</h2><p>UI file missing.</p>" | |
| return FileResponse(html_path, media_type="text/html") | |
| def get_current_song(): | |
| song = nowplaying.get_current_song() | |
| if song: | |
| return JSONResponse(content=song) | |
| return JSONResponse(content={"error": "No song playing"}, status_code=404) | |
| def stream_audio(): | |
| song = nowplaying.get_current_song() | |
| if not song or not song.get("path") or not os.path.exists(song["path"]): | |
| return JSONResponse(content={"error": "No song playing or file missing"}, status_code=404) | |
| file_path = song["path"] | |
| return StreamingResponse(get_audio_stream(file_path)(), media_type="audio/mpeg") | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |