Add library support

This commit is contained in:
TheKodeToad 2024-06-11 19:14:58 +01:00
parent 1d9b12b8a8
commit d3b64c6401
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
6 changed files with 92 additions and 55 deletions

View file

@ -37,6 +37,7 @@ dependencies {
implementation("com.h2database:h2:$h2_version") implementation("com.h2database:h2:$h2_version")
implementation("org.jetbrains.exposed:exposed-core:$exposed_version") implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version") implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
implementation("org.jetbrains.exposed:exposed-json:$exposed_version")
implementation("org.jetbrains.exposed:exposed-kotlin-datetime:$exposed_version") implementation("org.jetbrains.exposed:exposed-kotlin-datetime:$exposed_version")
implementation("io.ktor:ktor-server-netty-jvm") implementation("io.ktor:ktor-server-netty-jvm")
implementation("ch.qos.logback:logback-classic:$logback_version") implementation("ch.qos.logback:logback-classic:$logback_version")

View file

@ -4,4 +4,4 @@ logback_version=1.4.14
kotlin.code.style=official kotlin.code.style=official
postgres_version=42.7.3 postgres_version=42.7.3
h2_version=2.1.214 h2_version=2.1.214
exposed_version=0.41.1 exposed_version=0.51.1

View file

@ -7,10 +7,6 @@ import org.jetbrains.exposed.sql.transactions.transaction
object DB { object DB {
var db: Database? = null var db: Database? = null
private fun getEnv(name: String, default: String): String {
return System.getenv(name) ?: default
}
fun init(): Boolean { fun init(): Boolean {
// use postgresql // use postgresql
try { try {
@ -23,10 +19,10 @@ object DB {
) )
db = d db = d
transaction(db) { transaction(db) {
logger.info("what is going on")
SchemaUtils.create( SchemaUtils.create(
LoaderVersions, LoaderVersions,
LibraryVersions LibraryVersions,
File
) )
} }
if (db == null) { if (db == null) {
@ -43,24 +39,6 @@ object DB {
return true return true
} }
fun getLoaderVersions(): List<LoaderVersion> {
logger.info("Getting loader versions...")
if (db == null) {
logger.error("Database is null.")
init()
}
logger.info(db!!.name)
return transaction(db) {
return@transaction LoaderVersions.selectAll().map {
LoaderVersion(
it[LoaderVersions.version],
it[LoaderVersions.releaseDate],
it[LoaderVersions.downloadUrl]
)
}
}.sortedBy { it.releaseDate }.reversed()
}
fun getLibraryVersions(): List<LibraryVersion> { fun getLibraryVersions(): List<LibraryVersion> {
return transaction(db) { return transaction(db) {
return@transaction LibraryVersions.selectAll().map { return@transaction LibraryVersions.selectAll().map {

View file

@ -2,6 +2,7 @@ package dev.frogmc.plugins
import dev.frogmc.DB import dev.frogmc.DB
import dev.frogmc.types.LoaderVersion import dev.frogmc.types.LoaderVersion
import dev.frogmc.types.PartialLoaderVersion
import dev.frogmc.types.LoaderVersions import dev.frogmc.types.LoaderVersions
import dev.frogmc.types.ModrinthVersion import dev.frogmc.types.ModrinthVersion
import io.ktor.client.* import io.ktor.client.*
@ -14,13 +15,11 @@ import io.ktor.server.http.content.*
import io.ktor.server.request.* import io.ktor.server.request.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import kotlinx.datetime.LocalDateTime
import kotlinx.html.* import kotlinx.html.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.jetbrains.exposed.exceptions.ExposedSQLException import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
fun Application.configureRouting() { fun Application.configureRouting() {
@ -96,30 +95,62 @@ fun Application.configureRouting() {
route("/v1") { route("/v1") {
route("/loader") { route("/loader") {
get("/versions") { get("/versions") {
val versions = DB.getLoaderVersions() call.respond(transaction(DB.db) {
call.respond(versions) LoaderVersions
.select(LoaderVersions.version, LoaderVersions.releaseDate)
.orderBy(LoaderVersions.releaseDate to SortOrder.ASC)
.map {
PartialLoaderVersion(
it[LoaderVersions.version],
it[LoaderVersions.releaseDate],
)
}
})
} }
get("/versions/latest") { get("/versions/latest") {
val versions = DB.getLoaderVersions() val row = transaction(DB.db) {
call.respond(versions.first()) LoaderVersions
} .selectAll()
get("/versions/{version}/download") { .orderBy(LoaderVersions.releaseDate to SortOrder.DESC)
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest) .firstOrNull()
val versions = DB.getLoaderVersions() }
val versionObj = versions.find { it.version == version } ?: return@get call.respond(
HttpStatusCode.NotFound, if (row == null) {
"message" to "Version not found." call.respond(HttpStatusCode.NotFound)
return@get
}
call.respond(
LoaderVersion(
row[LoaderVersions.version],
row[LoaderVersions.releaseDate],
row[LoaderVersions.libraries]
)
) )
call.respond("url" to versionObj.downloadUrl)
} }
get("/versions/{version}") { get("/versions/{version}") {
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest) val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest)
val versions = DB.getLoaderVersions() val row = transaction {
val versionObj = LoaderVersions
versions.find { it.version == version } ?: return@get call.respond(HttpStatusCode.NotFound) .selectAll()
call.respond(versionObj) .where { LoaderVersions.version eq version }
.firstOrNull()
}
if (row == null) {
call.respond(HttpStatusCode.NotFound)
return@get
}
call.respond(
LoaderVersion(
row[LoaderVersions.version],
row[LoaderVersions.releaseDate],
row[LoaderVersions.libraries]
)
)
} }
route("/versions") { route("/versions/add") {
install(authPlugin) install(authPlugin)
post { post {
val versionObj = call.receive<LoaderVersion>() val versionObj = call.receive<LoaderVersion>()
@ -129,7 +160,7 @@ fun Application.configureRouting() {
LoaderVersions.insert { LoaderVersions.insert {
it[version] = versionObj.version it[version] = versionObj.version
it[releaseDate] = versionObj.releaseDate it[releaseDate] = versionObj.releaseDate
it[downloadUrl] = versionObj.downloadUrl it[libraries] = versionObj.libraries
} }
} }
} catch (e: ExposedSQLException) { // TODO: this catches EVERYTHING } catch (e: ExposedSQLException) { // TODO: this catches EVERYTHING
@ -139,16 +170,20 @@ fun Application.configureRouting() {
call.respond(HttpStatusCode.OK) call.respond(HttpStatusCode.OK)
} }
} }
route("/versions/{version}") { route("/versions/delete/{version}") {
install(authPlugin) install(authPlugin)
delete { delete {
val version = val version =
call.parameters["version"] ?: return@delete call.respond(HttpStatusCode.BadRequest) call.parameters["version"] ?: return@delete call.respond(HttpStatusCode.BadRequest)
transaction { val rows = transaction {
LoaderVersions.deleteWhere { LoaderVersions.deleteWhere { LoaderVersions.version eq version }
LoaderVersions.version eq version }
}
} if (rows == 0) {
call.respond(HttpStatusCode.NotFound)
return@delete
}
call.respond(HttpStatusCode.OK) call.respond(HttpStatusCode.OK)
} }
} }

View file

@ -4,11 +4,25 @@ import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable
data class PartialLoaderVersion(
val version: String,
val releaseDate: LocalDateTime,
)
@Serializable @Serializable
data class LoaderVersion( data class LoaderVersion(
val version: String, val version: String,
val releaseDate: LocalDateTime, val releaseDate: LocalDateTime,
val downloadUrl: String val libraries: List<LoaderLibrary>,
)
@Serializable
data class LoaderLibrary(
val name: String,
val url: String,
val sha1: String,
val size: Int,
) )
@Serializable @Serializable

View file

@ -1,12 +1,14 @@
package dev.frogmc.types package dev.frogmc.types
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.json.jsonb
import org.jetbrains.exposed.sql.kotlin.datetime.datetime import org.jetbrains.exposed.sql.kotlin.datetime.datetime
object LoaderVersions : Table() { object LoaderVersions : Table() {
val version = text("version") val version = text("version")
val releaseDate = datetime("release_date") val releaseDate = datetime("release_date")
val downloadUrl = varchar("download_url", 255) val libraries = jsonb<List<LoaderLibrary>>("libraries", Json)
override val primaryKey = PrimaryKey(version) override val primaryKey = PrimaryKey(version)
} }
@ -15,4 +17,11 @@ object LibraryVersions : Table() {
val releaseDate = datetime("release_date") val releaseDate = datetime("release_date")
val modrinthVersion = text("mr_version") val modrinthVersion = text("mr_version")
override val primaryKey = PrimaryKey(version) override val primaryKey = PrimaryKey(version)
} }
object File : Table() {
val version = text("version")
val sha1 = text("sha1")
val size = text("size")
val url = text("url")
}