feat: bootstrap MusicManager with scanner and Subsonic auth
- Gradle/Kotlin/Ktor project on JVM 21 with ShadowJar - SQLite WAL, Flyway migrations and config-driven users/folders - deterministic IDs, MP3 tag extraction, cover cache and delta scanner - OpenSubsonic JSON envelope with token/password authentication - system endpoints: ping, license, extensions, scan status
This commit is contained in:
1 parent
961341b449
commit
e194badefb
45 files changed
+4079
-126
No files matched your search
@@ -0,0 +1,109 @@
|
||||
package win.taptap.musicmanager
|
||||
|
||||
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.partialcontent.PartialContent
|
||||
import io.ktor.server.plugins.statuspages.StatusPages
|
||||
import io.ktor.server.request.path
|
||||
import io.ktor.server.response.respondText
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.routing
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.job
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.slf4j.LoggerFactory
|
||||
import win.taptap.musicmanager.config.AppConfig
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.ConfigRepository
|
||||
import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.MediaRepository
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.MediaScanner
|
||||
import win.taptap.musicmanager.subsonic.respondSubsonicError
|
||||
import win.taptap.musicmanager.subsonic.routes.subsonicRoutes
|
||||
|
||||
private val log = LoggerFactory.getLogger("win.taptap.musicmanager.Application")
|
||||
|
||||
fun main() {
|
||||
val config = AppConfig.fromEnv()
|
||||
|
||||
val library = LibraryConfig.load(config.libraryConfigPath).also { it.validatePaths() }
|
||||
val users = UsersConfig.load(config.usersFile).also { it.validateAgainst(library) }
|
||||
|
||||
val db = DatabaseFactory.connect(config)
|
||||
try {
|
||||
ConfigRepository.sync(library, users)
|
||||
|
||||
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
val scanner = MediaScanner(library, CoverArtService(config.dataDir))
|
||||
if (MediaRepository.countSongs() == 0L) {
|
||||
log.info("Song table is empty, starting initial library scan")
|
||||
scanner.start(applicationScope)
|
||||
}
|
||||
|
||||
val server = embeddedServer(Netty, port = config.port, host = "0.0.0.0") {
|
||||
install(ContentNegotiation) {
|
||||
json(
|
||||
Json {
|
||||
ignoreUnknownKeys = true
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
},
|
||||
)
|
||||
}
|
||||
install(PartialContent)
|
||||
install(AutoHeadResponse)
|
||||
install(CallLogging)
|
||||
install(StatusPages) {
|
||||
exception<Throwable> { call, cause ->
|
||||
log.error("Unhandled exception for ${call.request.path()}", cause)
|
||||
if (call.request.path().startsWith("/rest/")) {
|
||||
call.respondSubsonicError(0, "Internal server error")
|
||||
} else {
|
||||
call.respondText("Internal server error", status = HttpStatusCode.InternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
routing {
|
||||
get("/health") { call.respondText("ok") }
|
||||
subsonicRoutes(
|
||||
users = users,
|
||||
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 MusicManager on port {} with {} library folders and {} users",
|
||||
config.port,
|
||||
library.folders.size,
|
||||
users.users.size,
|
||||
)
|
||||
server.start(wait = true)
|
||||
} catch (t: Throwable) {
|
||||
runCatching { db.close() }
|
||||
throw t
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user