spotify-clone/backend/api/endpoints/settings.py

64 lines
2.6 KiB
Python

import subprocess
import os
import sys
from fastapi import APIRouter, HTTPException, BackgroundTasks
import logging
router = APIRouter()
logger = logging.getLogger(__name__)
def restart_server():
"""Restarts the server by killing the current process."""
logger.info("Restarting server...")
# This works in Docker if a restart policy is set (e.g., restart: always)
os.kill(os.getpid(), 15) # SIGTERM
@router.get("/check")
async def check_settings_health():
"""Debug endpoint to verify settings router is mounted."""
return {"status": "ok", "message": "Settings router is active"}
@router.post("/update-ytdlp")
async def update_ytdlp(background_tasks: BackgroundTasks):
try:
# Run pip install to upgrade yt-dlp to master
logger.info("Starting yt-dlp update...")
process = subprocess.run(
[sys.executable, "-m", "pip", "install", "--upgrade", "--force-reinstall", "--index-url", "https://pypi.org/simple", "git+https://github.com/yt-dlp/yt-dlp.git@master"],
capture_output=True,
text=True,
check=True
)
logger.info(f"Update Output: {process.stdout}")
# Schedule restart after a short delay to allow response to be sent
background_tasks.add_task(restart_server)
return {"status": "success", "message": "yt-dlp updated. Server restarting..."}
except subprocess.CalledProcessError as e:
logger.error(f"Update Failed: {e.stderr}")
raise HTTPException(status_code=500, detail=f"Update failed: {e.stderr}")
except Exception as e:
logger.error(f"Unexpected Error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/update-spotdl")
async def update_spotdl(background_tasks: BackgroundTasks):
try:
logger.info("Starting spotdl update...")
process = subprocess.run(
[sys.executable, "-m", "pip", "install", "--upgrade", "--index-url", "https://pypi.org/simple", "spotdl"],
capture_output=True,
text=True,
check=True
)
logger.info(f"Update Output: {process.stdout}")
background_tasks.add_task(restart_server)
return {"status": "success", "message": "spotdl updated. Server restarting..."}
except subprocess.CalledProcessError as e:
logger.error(f"Update Failed: {e.stderr}")
raise HTTPException(status_code=500, detail=f"Update failed: {e.stderr}")
except Exception as e:
logger.error(f"Unexpected Error: {e}")
raise HTTPException(status_code=500, detail=str(e))