Add library support
This commit is contained in:
parent
1d9b12b8a8
commit
d3b64c6401
|
@ -37,6 +37,7 @@ dependencies {
|
|||
implementation("com.h2database:h2:$h2_version")
|
||||
implementation("org.jetbrains.exposed:exposed-core:$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("io.ktor:ktor-server-netty-jvm")
|
||||
implementation("ch.qos.logback:logback-classic:$logback_version")
|
||||
|
|
|
@ -4,4 +4,4 @@ logback_version=1.4.14
|
|||
kotlin.code.style=official
|
||||
postgres_version=42.7.3
|
||||
h2_version=2.1.214
|
||||
exposed_version=0.41.1
|
||||
exposed_version=0.51.1
|
|
@ -7,10 +7,6 @@ import org.jetbrains.exposed.sql.transactions.transaction
|
|||
object DB {
|
||||
var db: Database? = null
|
||||
|
||||
private fun getEnv(name: String, default: String): String {
|
||||
return System.getenv(name) ?: default
|
||||
}
|
||||
|
||||
fun init(): Boolean {
|
||||
// use postgresql
|
||||
try {
|
||||
|
@ -23,10 +19,10 @@ object DB {
|
|||
)
|
||||
db = d
|
||||
transaction(db) {
|
||||
logger.info("what is going on")
|
||||
SchemaUtils.create(
|
||||
LoaderVersions,
|
||||
LibraryVersions
|
||||
LibraryVersions,
|
||||
File
|
||||
)
|
||||
}
|
||||
if (db == null) {
|
||||
|
@ -43,24 +39,6 @@ object DB {
|
|||
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> {
|
||||
return transaction(db) {
|
||||
return@transaction LibraryVersions.selectAll().map {
|
||||
|
|
|
@ -2,6 +2,7 @@ package dev.frogmc.plugins
|
|||
|
||||
import dev.frogmc.DB
|
||||
import dev.frogmc.types.LoaderVersion
|
||||
import dev.frogmc.types.PartialLoaderVersion
|
||||
import dev.frogmc.types.LoaderVersions
|
||||
import dev.frogmc.types.ModrinthVersion
|
||||
import io.ktor.client.*
|
||||
|
@ -14,13 +15,11 @@ import io.ktor.server.http.content.*
|
|||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.html.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.jetbrains.exposed.exceptions.ExposedSQLException
|
||||
import org.jetbrains.exposed.sql.*
|
||||
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
|
||||
|
||||
fun Application.configureRouting() {
|
||||
|
@ -96,30 +95,62 @@ fun Application.configureRouting() {
|
|||
route("/v1") {
|
||||
route("/loader") {
|
||||
get("/versions") {
|
||||
val versions = DB.getLoaderVersions()
|
||||
call.respond(versions)
|
||||
call.respond(transaction(DB.db) {
|
||||
LoaderVersions
|
||||
.select(LoaderVersions.version, LoaderVersions.releaseDate)
|
||||
.orderBy(LoaderVersions.releaseDate to SortOrder.ASC)
|
||||
.map {
|
||||
PartialLoaderVersion(
|
||||
it[LoaderVersions.version],
|
||||
it[LoaderVersions.releaseDate],
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
get("/versions/latest") {
|
||||
val versions = DB.getLoaderVersions()
|
||||
call.respond(versions.first())
|
||||
}
|
||||
get("/versions/{version}/download") {
|
||||
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest)
|
||||
val versions = DB.getLoaderVersions()
|
||||
val versionObj = versions.find { it.version == version } ?: return@get call.respond(
|
||||
HttpStatusCode.NotFound,
|
||||
"message" to "Version not found."
|
||||
val row = transaction(DB.db) {
|
||||
LoaderVersions
|
||||
.selectAll()
|
||||
.orderBy(LoaderVersions.releaseDate to SortOrder.DESC)
|
||||
.firstOrNull()
|
||||
}
|
||||
|
||||
if (row == null) {
|
||||
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}") {
|
||||
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest)
|
||||
val versions = DB.getLoaderVersions()
|
||||
val versionObj =
|
||||
versions.find { it.version == version } ?: return@get call.respond(HttpStatusCode.NotFound)
|
||||
call.respond(versionObj)
|
||||
val row = transaction {
|
||||
LoaderVersions
|
||||
.selectAll()
|
||||
.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)
|
||||
post {
|
||||
val versionObj = call.receive<LoaderVersion>()
|
||||
|
@ -129,7 +160,7 @@ fun Application.configureRouting() {
|
|||
LoaderVersions.insert {
|
||||
it[version] = versionObj.version
|
||||
it[releaseDate] = versionObj.releaseDate
|
||||
it[downloadUrl] = versionObj.downloadUrl
|
||||
it[libraries] = versionObj.libraries
|
||||
}
|
||||
}
|
||||
} catch (e: ExposedSQLException) { // TODO: this catches EVERYTHING
|
||||
|
@ -139,16 +170,20 @@ fun Application.configureRouting() {
|
|||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
route("/versions/{version}") {
|
||||
route("/versions/delete/{version}") {
|
||||
install(authPlugin)
|
||||
delete {
|
||||
val version =
|
||||
call.parameters["version"] ?: return@delete call.respond(HttpStatusCode.BadRequest)
|
||||
transaction {
|
||||
LoaderVersions.deleteWhere {
|
||||
LoaderVersions.version eq version
|
||||
}
|
||||
}
|
||||
val rows = transaction {
|
||||
LoaderVersions.deleteWhere { LoaderVersions.version eq version }
|
||||
}
|
||||
|
||||
if (rows == 0) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
return@delete
|
||||
}
|
||||
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,25 @@ import kotlinx.datetime.LocalDateTime
|
|||
import kotlinx.serialization.Serializable
|
||||
|
||||
|
||||
@Serializable
|
||||
data class PartialLoaderVersion(
|
||||
val version: String,
|
||||
val releaseDate: LocalDateTime,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class LoaderVersion(
|
||||
val version: String,
|
||||
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
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package dev.frogmc.types
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
import org.jetbrains.exposed.sql.json.jsonb
|
||||
import org.jetbrains.exposed.sql.kotlin.datetime.datetime
|
||||
|
||||
object LoaderVersions : Table() {
|
||||
val version = text("version")
|
||||
val releaseDate = datetime("release_date")
|
||||
val downloadUrl = varchar("download_url", 255)
|
||||
val libraries = jsonb<List<LoaderLibrary>>("libraries", Json)
|
||||
override val primaryKey = PrimaryKey(version)
|
||||
}
|
||||
|
||||
|
@ -15,4 +17,11 @@ object LibraryVersions : Table() {
|
|||
val releaseDate = datetime("release_date")
|
||||
val modrinthVersion = text("mr_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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue