Files
sdb2-backend/app/main.py
JSC 1b0d291ad3 Add comprehensive tests for player API endpoints and player service functionality
- Implemented tests for player API endpoints including play, pause, stop, next, previous, seek, set volume, set mode, reload playlist, and get state.
- Added mock player service for testing API interactions.
- Created tests for player service methods including play, pause, stop playback, next, previous, seek, set volume, set mode, and reload playlist.
- Ensured proper handling of success, error, and edge cases in both API and service tests.
- Verified state management and serialization in player state tests.
2025-07-30 01:22:24 +02:00

71 lines
2.0 KiB
Python

from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
import socketio
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api import api_router
from app.core.database import get_session_factory, init_db
from app.core.logging import get_logger, setup_logging
from app.middleware.logging import LoggingMiddleware
from app.services.extraction_processor import extraction_processor
from app.services.player import initialize_player_service, shutdown_player_service
from app.services.socket import socket_manager
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
"""Application lifespan context manager for setup and teardown."""
setup_logging()
logger = get_logger(__name__)
logger.info("Starting application")
await init_db()
logger.info("Database initialized")
# Start the extraction processor
await extraction_processor.start()
logger.info("Extraction processor started")
# Start the player service
await initialize_player_service(get_session_factory())
logger.info("Player service started")
yield
logger.info("Shutting down application")
# Stop the player service
await shutdown_player_service()
logger.info("Player service stopped")
# Stop the extraction processor
await extraction_processor.stop()
logger.info("Extraction processor stopped")
def create_app():
"""Create and configure the FastAPI application."""
app = FastAPI(lifespan=lifespan)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:8001"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(LoggingMiddleware)
# Include API routes
app.include_router(api_router)
# Create Socket.IO app with fallback to FastAPI app
return socketio.ASGIApp(socket_manager.sio, app)
app = create_app()