- V8 wipe migration and the shared song store keyed by YouTube video id - media store, indexer, downloader, recommendation top-up and refcounted GC - mood songs plus the recommendation lifecycle and mixed radio pools - new search, song and recommendation API; remove library/longform/seed routes - chart PVC/media env, docs, Nix, README and an end-to-end smoke test
230 lines
9.0 KiB
Kotlin
230 lines
9.0 KiB
Kotlin
package win.taptap.musicmanager
|
|
|
|
import io.ktor.http.ContentType
|
|
import io.ktor.http.HttpHeaders
|
|
import io.ktor.http.HttpMethod
|
|
import io.ktor.http.HttpStatusCode
|
|
import io.ktor.serialization.kotlinx.json.json
|
|
import io.ktor.server.application.install
|
|
import io.ktor.server.engine.embeddedServer
|
|
import io.ktor.server.netty.Netty
|
|
import io.ktor.server.plugins.autohead.AutoHeadResponse
|
|
import io.ktor.server.plugins.calllogging.CallLogging
|
|
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
|
import io.ktor.server.plugins.cors.routing.CORS
|
|
import io.ktor.server.plugins.partialcontent.PartialContent
|
|
import io.ktor.server.plugins.statuspages.StatusPages
|
|
import io.ktor.server.request.path
|
|
import io.ktor.server.response.respond
|
|
import io.ktor.server.response.respondText
|
|
import io.ktor.server.routing.get
|
|
import io.ktor.server.routing.routing
|
|
import java.nio.file.Files
|
|
import java.util.concurrent.TimeUnit
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.SupervisorJob
|
|
import kotlinx.coroutines.cancelAndJoin
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.isActive
|
|
import kotlinx.coroutines.job
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.runBlocking
|
|
import kotlinx.serialization.json.Json
|
|
import org.slf4j.LoggerFactory
|
|
import win.taptap.musicmanager.api.ApiError
|
|
import win.taptap.musicmanager.api.ApiErrorBody
|
|
import win.taptap.musicmanager.api.ApiException
|
|
import win.taptap.musicmanager.api.apiRoutes
|
|
import win.taptap.musicmanager.config.AppConfig
|
|
import win.taptap.musicmanager.config.UsersConfig
|
|
import win.taptap.musicmanager.db.ConfigRepository
|
|
import win.taptap.musicmanager.db.DatabaseFactory
|
|
import win.taptap.musicmanager.db.MoodRepository
|
|
import win.taptap.musicmanager.discovery.DiscoveryService
|
|
import win.taptap.musicmanager.discovery.TopUpService
|
|
import win.taptap.musicmanager.discovery.YtDlpSearch
|
|
import win.taptap.musicmanager.download.DownloadService
|
|
import win.taptap.musicmanager.download.YtDlpDownloader
|
|
import win.taptap.musicmanager.media.MediaGcService
|
|
import win.taptap.musicmanager.media.MediaStore
|
|
import win.taptap.musicmanager.radio.RadioService
|
|
import win.taptap.musicmanager.scanner.CoverArtService
|
|
import win.taptap.musicmanager.scanner.MediaScanner
|
|
import win.taptap.musicmanager.scanner.SongIndexer
|
|
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
|
|
|
private val log = LoggerFactory.getLogger("win.taptap.musicmanager.Application")
|
|
|
|
fun main() {
|
|
val config = AppConfig.fromEnv()
|
|
val users = UsersConfig.load(config.usersFile)
|
|
|
|
Files.createDirectories(config.dataDir)
|
|
Files.createDirectories(config.mediaDir)
|
|
Files.createDirectories(config.downloadDir)
|
|
|
|
val db = DatabaseFactory.connect(config)
|
|
try {
|
|
ConfigRepository.sync(users)
|
|
|
|
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
|
val coverArtService = CoverArtService(config.dataDir)
|
|
val mediaStore = MediaStore(config.mediaDir)
|
|
val indexer = SongIndexer(mediaStore, coverArtService)
|
|
val gc = MediaGcService(mediaStore, coverArtService)
|
|
val transcoder = FfmpegTranscoder(config.ffmpegPath)
|
|
val radioService = RadioService()
|
|
val ytSearch = YtDlpSearch(config.ytDlpPath)
|
|
val discoveryService = DiscoveryService(ytSearch)
|
|
val topUpService = TopUpService(discoveryService, applicationScope)
|
|
val scanner = MediaScanner(mediaStore, indexer)
|
|
val downloadService = DownloadService(
|
|
downloader = YtDlpDownloader(config.ytDlpPath),
|
|
mediaStore = mediaStore,
|
|
indexer = indexer,
|
|
gc = gc,
|
|
tempDir = config.downloadDir,
|
|
)
|
|
|
|
// The media root is the only source of songs, so index it at startup.
|
|
log.info("Scanning media root {}", config.mediaDir)
|
|
scanner.start(applicationScope)
|
|
|
|
applicationScope.launch(Dispatchers.IO) {
|
|
delay(TimeUnit.SECONDS.toMillis(5))
|
|
while (isActive) {
|
|
val worked = runCatching { downloadService.processOne() }
|
|
.onFailure { log.warn("Download worker error", it) }
|
|
.getOrDefault(false)
|
|
delay(if (worked) 1_000 else 15_000)
|
|
}
|
|
}
|
|
|
|
// Refill recommendation slots and run due discovery passes.
|
|
applicationScope.launch(Dispatchers.IO) {
|
|
delay(TimeUnit.MINUTES.toMillis(1))
|
|
while (isActive) {
|
|
runCatching {
|
|
MoodRepository.all()
|
|
.filter { it.recommendationCount > 0 }
|
|
.forEach { mood ->
|
|
topUpService.topUp(mood, force = false)
|
|
}
|
|
}.onFailure { log.warn("Background recommendation top-up failed", it) }
|
|
delay(TimeUnit.HOURS.toMillis(1))
|
|
}
|
|
}
|
|
|
|
// Delete files that no mood, recommendation or open session references.
|
|
applicationScope.launch(Dispatchers.IO) {
|
|
delay(TimeUnit.MINUTES.toMillis(10))
|
|
while (isActive) {
|
|
runCatching { gc.collectGarbage() }
|
|
.onFailure { log.warn("Media GC failed", it) }
|
|
delay(TimeUnit.HOURS.toMillis(1))
|
|
}
|
|
}
|
|
|
|
val server = embeddedServer(Netty, port = config.port, host = "0.0.0.0") {
|
|
install(ContentNegotiation) {
|
|
json(
|
|
Json {
|
|
ignoreUnknownKeys = true
|
|
encodeDefaults = true
|
|
explicitNulls = false
|
|
},
|
|
)
|
|
}
|
|
// The Wasm client can be hosted separately; bearer auth needs no cookies.
|
|
install(CORS) {
|
|
anyHost()
|
|
allowMethod(HttpMethod.Get)
|
|
allowMethod(HttpMethod.Post)
|
|
allowMethod(HttpMethod.Patch)
|
|
allowMethod(HttpMethod.Delete)
|
|
allowMethod(HttpMethod.Head)
|
|
allowMethod(HttpMethod.Options)
|
|
allowHeader(HttpHeaders.Authorization)
|
|
allowHeader(HttpHeaders.ContentType)
|
|
allowHeader(HttpHeaders.Range)
|
|
allowNonSimpleContentTypes = true
|
|
maxAgeInSeconds = 3600
|
|
}
|
|
install(PartialContent)
|
|
install(AutoHeadResponse)
|
|
install(CallLogging)
|
|
install(StatusPages) {
|
|
exception<ApiException> { call, cause ->
|
|
call.respond(
|
|
cause.status,
|
|
ApiErrorBody(
|
|
ApiError(cause.code, cause.message),
|
|
),
|
|
)
|
|
}
|
|
exception<Throwable> { call, cause ->
|
|
log.error("Unhandled exception for ${call.request.path()}", cause)
|
|
if (call.request.path().startsWith("/api/")) {
|
|
call.respond(
|
|
HttpStatusCode.InternalServerError,
|
|
ApiErrorBody(
|
|
ApiError(
|
|
"internal",
|
|
"Internal server error",
|
|
),
|
|
),
|
|
)
|
|
} else {
|
|
call.respondText("Internal server error", status = HttpStatusCode.InternalServerError)
|
|
}
|
|
}
|
|
}
|
|
|
|
routing {
|
|
get("/health") { call.respondText("ok") }
|
|
get("/") {
|
|
call.respondText(
|
|
"AudioManager\nAPI: /api/v1\nHealth: /health\n",
|
|
ContentType.Text.Plain,
|
|
)
|
|
}
|
|
apiRoutes(
|
|
users = users,
|
|
coverArtService = coverArtService,
|
|
transcoder = transcoder,
|
|
radioService = radioService,
|
|
discoveryService = discoveryService,
|
|
topUpService = topUpService,
|
|
ytSearch = ytSearch,
|
|
ytResolver = ytSearch,
|
|
mediaStore = mediaStore,
|
|
gc = gc,
|
|
startScan = { scanner.start(applicationScope) },
|
|
scanStatus = { scanner.status() },
|
|
)
|
|
}
|
|
}
|
|
|
|
val scopeJob = applicationScope.coroutineContext.job
|
|
Runtime.getRuntime().addShutdownHook(
|
|
Thread {
|
|
runBlocking { scopeJob.cancelAndJoin() }
|
|
runCatching { server.stop(1_000, 5_000) }
|
|
runCatching { db.close() }
|
|
},
|
|
)
|
|
|
|
log.info(
|
|
"Starting AudioManager on port {} with {} users and media root {}",
|
|
config.port,
|
|
users.users.size,
|
|
config.mediaDir,
|
|
)
|
|
server.start(wait = true)
|
|
} catch (t: Throwable) {
|
|
runCatching { db.close() }
|
|
throw t
|
|
}
|
|
}
|