mirror of
https://github.com/wahyd4/you-music.git
synced 2026-08-08 20:59:47 +10:00
69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
import os
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from app.api import music, playlist, download, search, stream, artist
|
|
from app.core.config import settings
|
|
from app.db.session import init_db
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup
|
|
os.makedirs(settings.MUSIC_DIR, exist_ok=True)
|
|
os.makedirs(settings.UPLOAD_DIR, exist_ok=True)
|
|
os.makedirs(os.path.join(settings.BASE_DIR, "data", "cache"), exist_ok=True)
|
|
os.makedirs(os.path.join(settings.BASE_DIR, "data", "cache", "artist_images"), exist_ok=True)
|
|
await init_db()
|
|
yield
|
|
# Shutdown
|
|
|
|
|
|
app = FastAPI(
|
|
title="YouMusic API",
|
|
description="A modern web music player with download capabilities",
|
|
version="1.0.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Mount static files
|
|
os.makedirs(settings.MUSIC_DIR, exist_ok=True)
|
|
app.mount("/music", StaticFiles(directory=settings.MUSIC_DIR), name="music")
|
|
|
|
# Mount cache directory for artist images
|
|
cache_dir = os.path.join(settings.BASE_DIR, "data", "cache")
|
|
os.makedirs(cache_dir, exist_ok=True)
|
|
app.mount("/cache", StaticFiles(directory=cache_dir), name="cache")
|
|
|
|
# Include routers
|
|
app.include_router(music.router, prefix="/api/music", tags=["music"])
|
|
app.include_router(playlist.router, prefix="/api/playlists", tags=["playlists"])
|
|
app.include_router(download.router, prefix="/api/download", tags=["download"])
|
|
app.include_router(search.router, prefix="/api/search", tags=["search"])
|
|
app.include_router(stream.router, prefix="/api", tags=["stream"])
|
|
app.include_router(artist.router, prefix="/api/artists", tags=["artists"])
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "YouMusic API is running"}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "healthy"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|