Add library support
This commit is contained in:
parent
1d9b12b8a8
commit
d3b64c6401
|
@ -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")
|
||||||
|
|
|
@ -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
|
|
@ -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 {
|
||||||
|
|
|
@ -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()
|
||||||
|
.orderBy(LoaderVersions.releaseDate to SortOrder.DESC)
|
||||||
|
.firstOrNull()
|
||||||
}
|
}
|
||||||
get("/versions/{version}/download") {
|
|
||||||
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest)
|
if (row == null) {
|
||||||
val versions = DB.getLoaderVersions()
|
call.respond(HttpStatusCode.NotFound)
|
||||||
val versionObj = versions.find { it.version == version } ?: return@get call.respond(
|
return@get
|
||||||
HttpStatusCode.NotFound,
|
}
|
||||||
"message" to "Version not found."
|
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
route("/versions") {
|
|
||||||
|
if (row == null) {
|
||||||
|
call.respond(HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
}
|
||||||
|
|
||||||
|
call.respond(
|
||||||
|
LoaderVersion(
|
||||||
|
row[LoaderVersions.version],
|
||||||
|
row[LoaderVersions.releaseDate],
|
||||||
|
row[LoaderVersions.libraries]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,3 +18,10 @@ object LibraryVersions : Table() {
|
||||||
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")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue