Compare commits
No commits in common. "2c8016e77f5503f3f9be6c7932a9dede6cddc1bf" and "13487b5234509b577481fb75a022a31bb1737585" have entirely different histories.
2c8016e77f
...
13487b5234
|
@ -37,7 +37,6 @@ 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.51.1
|
||||
exposed_version=0.41.1
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package dev.frogmc
|
||||
|
||||
object Config {
|
||||
val POSTGRES_DATABASE = getEnv("DATABASE", "frogmc")
|
||||
val POSTGRES_USER = getEnv("USER", "postgres")
|
||||
val POSTGRES_PASSWORD = getEnv("PASSWORD", "example")
|
||||
val POSTGRES_HOST = getEnv("HOST", "localhost")
|
||||
val POSTGRES_PORT = getEnv("PORT", "5432")
|
||||
val UPLOAD_SECRET = getEnv("UPLOAD_SECRET", "").toByteArray()
|
||||
|
||||
private fun getEnv(key: String, default: String): String {
|
||||
return System.getenv("FROGMC_META_$key") ?: default
|
||||
}
|
||||
}
|
|
@ -7,22 +7,32 @@ 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 {
|
||||
val database = getEnv("FROGMC_META_DATABASE", "frogmc")
|
||||
val user = getEnv("FROGMC_META_USER", "postgres")
|
||||
val password = getEnv("FROGMC_META_PASSWORD", "example")
|
||||
val host = getEnv("FROGMC_META_HOST", "localhost")
|
||||
val port = getEnv("FROGMC_META_PORT", "5432")
|
||||
|
||||
logger.info("Connecting to the database...")
|
||||
val d = Database.connect(
|
||||
url = "jdbc:postgresql://${Config.POSTGRES_HOST}:${Config.POSTGRES_PORT}/${Config.POSTGRES_DATABASE}",
|
||||
url = "jdbc:postgresql://$host:$port/$database",
|
||||
driver = "org.postgresql.Driver",
|
||||
Config.POSTGRES_USER,
|
||||
Config.POSTGRES_PASSWORD
|
||||
user,
|
||||
password
|
||||
)
|
||||
db = d
|
||||
transaction(db) {
|
||||
logger.info("what is going on")
|
||||
SchemaUtils.create(
|
||||
LoaderVersions,
|
||||
LibraryVersions,
|
||||
File
|
||||
LibraryVersions
|
||||
)
|
||||
}
|
||||
if (db == null) {
|
||||
|
@ -35,10 +45,32 @@ object DB {
|
|||
logger.error(e.stackTraceToString())
|
||||
return false
|
||||
}
|
||||
if (db == null) {
|
||||
logger.error("Failed to connect to the database.")
|
||||
return false
|
||||
}
|
||||
logger.info(db!!.name)
|
||||
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 {
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package dev.frogmc.plugins
|
||||
|
||||
import dev.frogmc.Config
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.response.*
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.security.MessageDigest
|
||||
|
||||
val authPlugin = createRouteScopedPlugin("auth") {
|
||||
onCall {
|
||||
val authorization = it.request.headers["Authorization"]
|
||||
if (authorization.isNullOrEmpty() || !MessageDigest.isEqual(authorization.toByteArray(), Config.UPLOAD_SECRET))
|
||||
it.respond(HttpStatusCode.Unauthorized);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,6 @@
|
|||
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.*
|
||||
import io.ktor.client.call.*
|
||||
|
@ -12,113 +9,108 @@ import io.ktor.http.*
|
|||
import io.ktor.server.application.*
|
||||
import io.ktor.server.html.*
|
||||
import io.ktor.server.http.content.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
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.transactions.transaction
|
||||
|
||||
fun BODY.route(block: DIV.() -> Unit) =
|
||||
div(classes = "route", block)
|
||||
|
||||
|
||||
fun Application.configureRouting() {
|
||||
routing {
|
||||
staticResources("/", "static")
|
||||
staticResources("/static", "static")
|
||||
get("/") {
|
||||
call.respondHtml {
|
||||
head {
|
||||
title { +"FrogMC API" }
|
||||
link { rel = "stylesheet"; type = "text/css"; href = "/static/style.css" }
|
||||
}
|
||||
body {
|
||||
h1 { +"FrogMC API" }
|
||||
p { +"This is the FrogMC Meta API." }
|
||||
p { +"Use this to fetch versions of FrogMC library and loader." }
|
||||
br()
|
||||
p { +"Current API version: v1" }
|
||||
br()
|
||||
div(classes = "routes") {
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/loader/versions" }
|
||||
p { +"Fetches all versions of FrogMC loader." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/loader/versions/latest" }
|
||||
p { +"Fetches the latest version of FrogMC loader." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/loader/versions/{version}" }
|
||||
p { +"Fetches a specific version of FrogMC loader." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/loader/versions/{version}/download" }
|
||||
p { +"Fetches the download URL of a specific version of FrogMC loader." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/library/versions" }
|
||||
p { +"Fetches all versions of FrogMC library." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/library/versions/latest" }
|
||||
p { +"Fetches the latest version of FrogMC library." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/library/versions/{version}" }
|
||||
p { +"Fetches a specific version of FrogMC library." }
|
||||
}
|
||||
br()
|
||||
div(classes = "route") {
|
||||
code { +"GET /v1/library/versions/{version}/download" }
|
||||
p { +"Fetches the download URL of a specific version of FrogMC library." }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
route("/v1") {
|
||||
route("/loader") {
|
||||
get("/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],
|
||||
)
|
||||
}
|
||||
})
|
||||
val versions = DB.getLoaderVersions()
|
||||
call.respond(versions)
|
||||
}
|
||||
get("/versions/latest") {
|
||||
val row = transaction(DB.db) {
|
||||
LoaderVersions
|
||||
.selectAll()
|
||||
.orderBy(LoaderVersions.releaseDate to SortOrder.DESC)
|
||||
.firstOrNull()
|
||||
val versions = DB.getLoaderVersions()
|
||||
call.respond(versions.first())
|
||||
}
|
||||
|
||||
if (row == null) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
return@get
|
||||
}
|
||||
|
||||
call.respond(
|
||||
LoaderVersion(
|
||||
row[LoaderVersions.version],
|
||||
row[LoaderVersions.releaseDate],
|
||||
row[LoaderVersions.libraries]
|
||||
get("/versions/{version}/download") {
|
||||
val version = call.parameters["version"] ?: return@get call.respond(
|
||||
HttpStatusCode.BadRequest,
|
||||
"message" to "Invalid version."
|
||||
)
|
||||
val versions = DB.getLoaderVersions()
|
||||
val versionObj = versions.find { it.version == version } ?: return@get call.respond(
|
||||
HttpStatusCode.NotFound,
|
||||
"message" to "Version not found."
|
||||
)
|
||||
call.respond("url" to versionObj.downloadUrl)
|
||||
}
|
||||
get("/versions/{version}") {
|
||||
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest)
|
||||
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]
|
||||
val version = call.parameters["version"] ?: return@get call.respond(
|
||||
HttpStatusCode.BadRequest,
|
||||
"message" to "Invalid version."
|
||||
)
|
||||
val versions = DB.getLoaderVersions()
|
||||
val versionObj = versions.find { it.version == version } ?: return@get call.respond(
|
||||
HttpStatusCode.NotFound,
|
||||
"message" to "Version not found."
|
||||
)
|
||||
}
|
||||
route("/versions/upload") {
|
||||
install(authPlugin)
|
||||
post {
|
||||
val versionObj = call.receive<LoaderVersion>()
|
||||
|
||||
try {
|
||||
transaction {
|
||||
LoaderVersions.insert {
|
||||
it[version] = versionObj.version
|
||||
it[releaseDate] = versionObj.releaseDate
|
||||
it[libraries] = versionObj.libraries
|
||||
}
|
||||
}
|
||||
} catch (e: ExposedSQLException) { // TODO: this catches EVERYTHING
|
||||
call.respond(HttpStatusCode.Conflict)
|
||||
return@post
|
||||
}
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
route("/versions/delete/{version}") {
|
||||
install(authPlugin)
|
||||
delete {
|
||||
val version =
|
||||
call.parameters["version"] ?: return@delete call.respond(HttpStatusCode.BadRequest)
|
||||
val rows = transaction {
|
||||
LoaderVersions.deleteWhere { LoaderVersions.version eq version }
|
||||
}
|
||||
|
||||
if (rows == 0) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
return@delete
|
||||
}
|
||||
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
call.respond(versionObj)
|
||||
}
|
||||
}
|
||||
route("/library") {
|
||||
|
|
|
@ -4,25 +4,11 @@ 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 libraries: List<LoaderLibrary>,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class LoaderLibrary(
|
||||
val name: String,
|
||||
val url: String,
|
||||
val sha1: String,
|
||||
val size: Int,
|
||||
val downloadUrl: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
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 libraries = jsonb<List<LoaderLibrary>>("libraries", Json)
|
||||
val downloadUrl = varchar("download_url", 255)
|
||||
override val primaryKey = PrimaryKey(version)
|
||||
}
|
||||
|
||||
|
@ -18,10 +16,3 @@ object LibraryVersions : Table() {
|
|||
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")
|
||||
}
|
||||
|
|
|
@ -1,68 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>FrogMC API</title>
|
||||
<link rel="stylesheet" type="text/css" href="/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>FrogMC API</h1>
|
||||
<p>This is the FrogMC Meta API.</p>
|
||||
<p>Use this to fetch versions of FrogMC library and loader.</p>
|
||||
<br>
|
||||
<p>Current API version: v1</p>
|
||||
<br>
|
||||
<div class="routes">
|
||||
<div class="route">
|
||||
<code>GET /v1/loader/versions</code>
|
||||
<p>Fetches all versions of FrogMC loader.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/loader/versions/latest</code>
|
||||
<p>Fetches the latest version of FrogMC loader.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/loader/versions/{version}</code>
|
||||
<p>Fetches a specific version of FrogMC loader.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/loader/versions/{version}/download</code>
|
||||
<p>Fetches the download URL of a specific version of FrogMC loader.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>POST /v1/loader/versions/upload</code>
|
||||
<p>Uploads a version of the loader.</p>
|
||||
<p><strong>This endpoint requires authorization.</strong></p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>DELETE /v1/loader/versions/delete/{version}</code>
|
||||
<p>Deletes a version of the loader.</p>
|
||||
<p><strong>This endpoint requires authorization.</strong></p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/library/versions</code>
|
||||
<p>Fetches all versions of FrogMC library.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/library/versions/latest</code>
|
||||
<p>Fetches the latest version of FrogMC library.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/library/versions/{version}</code>
|
||||
<p>Fetches a specific version of FrogMC library.</p>
|
||||
</div>
|
||||
<br>
|
||||
<div class="route">
|
||||
<code>GET /v1/library/versions/{version}/download</code>
|
||||
<p>Fetches the download URL of a specific version of FrogMC library.</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello Ktor!</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -18,6 +18,7 @@ div.routes {
|
|||
padding: 1em;
|
||||
background-color: var(--ctp-mocha-surface0);
|
||||
max-width: 400px;
|
||||
margin-left: 38.5%;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue