Compare commits

..

No commits in common. "mistress" and "TheKodeToad/upload" have entirely different histories.

14 changed files with 57 additions and 66 deletions

View file

@ -14,7 +14,7 @@ group = "dev.frogmc"
version = "0.0.1"
application {
mainClass.set("dev.frogmc.meta.ApplicationKt")
mainClass.set("dev.frogmc.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View file

@ -1,15 +1,12 @@
package dev.frogmc.meta
package dev.frogmc
import dev.frogmc.meta.plugins.configureHTTP
import dev.frogmc.meta.plugins.configureRouting
import dev.frogmc.meta.plugins.configureSerialization
import dev.frogmc.plugins.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
val logger: Logger = LoggerFactory.getLogger("FrogMC/Meta")
val logger = LoggerFactory.getLogger("FrogMC/Meta")
fun main() {

View file

@ -1,4 +1,4 @@
package dev.frogmc.meta
package dev.frogmc
object Config {
val POSTGRES_DATABASE = getEnv("DATABASE", "frogmc")
@ -6,9 +6,9 @@ object Config {
val POSTGRES_PASSWORD = getEnv("PASSWORD", "example")
val POSTGRES_HOST = getEnv("HOST", "localhost")
val POSTGRES_PORT = getEnv("PORT", "5432")
val UPLOAD_SECRET = getEnv("UPLOAD_SECRET", "")
val UPLOAD_SECRET = getEnv("UPLOAD_SECRET", "").toByteArray()
private fun getEnv(key: String, default: String): String {
return System.getenv("FROGMC_META_$key") ?: default
}
}
}

View file

@ -1,8 +1,6 @@
package dev.frogmc.meta
package dev.frogmc
import dev.frogmc.meta.types.LibraryVersion
import dev.frogmc.meta.types.LibraryVersions
import dev.frogmc.meta.types.LoaderVersions
import dev.frogmc.types.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
@ -23,7 +21,8 @@ object DB {
transaction(db) {
SchemaUtils.create(
LoaderVersions,
LibraryVersions
LibraryVersions,
File
)
}
if (db == null) {

View file

@ -1,20 +1,16 @@
package dev.frogmc.meta.plugins
package dev.frogmc.plugins
import dev.frogmc.meta.Config
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.toByteArray()
)
) {
it.respond(HttpStatusCode.Unauthorized)
}
if (authorization.isNullOrEmpty() || !MessageDigest.isEqual(authorization.toByteArray(), Config.UPLOAD_SECRET))
it.respond(HttpStatusCode.Unauthorized);
}
}
}

View file

@ -1,16 +1,18 @@
package dev.frogmc.meta.plugins
package dev.frogmc.plugins
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.response.*
fun Application.configureHTTP() {
install(CORS) {
anyHost()
allowMethod(HttpMethod.Options)
allowMethod(HttpMethod.Put)
allowMethod(HttpMethod.Delete)
allowMethod(HttpMethod.Patch)
allowHeader(HttpHeaders.Authorization)
allowHeader("MyCustomHeader")
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
}
}

View file

@ -1,19 +1,21 @@
package dev.frogmc.meta.plugins
package dev.frogmc.plugins
import dev.frogmc.meta.DB
import dev.frogmc.meta.types.LoaderVersion
import dev.frogmc.meta.types.PartialLoaderVersion
import dev.frogmc.meta.types.LoaderVersions
import dev.frogmc.meta.types.ModrinthVersion
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.*
import io.ktor.client.request.*
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.*
@ -29,7 +31,7 @@ fun Application.configureRouting() {
call.respond(transaction(DB.db) {
LoaderVersions
.select(LoaderVersions.version, LoaderVersions.releaseDate)
.orderBy(LoaderVersions.releaseDate to SortOrder.DESC)
.orderBy(LoaderVersions.releaseDate to SortOrder.ASC)
.map {
PartialLoaderVersion(
it[LoaderVersions.version],
@ -38,7 +40,7 @@ fun Application.configureRouting() {
}
})
}
get("/version/latest") {
get("/versions/latest") {
val row = transaction(DB.db) {
LoaderVersions
.selectAll()
@ -59,7 +61,7 @@ fun Application.configureRouting() {
)
)
}
get("/version/{version}") {
get("/versions/{version}") {
val version = call.parameters["version"] ?: return@get call.respond(HttpStatusCode.BadRequest)
val row = transaction {
LoaderVersions
@ -81,7 +83,7 @@ fun Application.configureRouting() {
)
)
}
route("/version/upload") {
route("/versions/upload") {
install(authPlugin)
post {
val versionObj = call.receive<LoaderVersion>()
@ -101,7 +103,7 @@ fun Application.configureRouting() {
call.respond(HttpStatusCode.OK)
}
}
route("/version/delete/{version}") {
route("/versions/delete/{version}") {
install(authPlugin)
delete {
val version =
@ -124,11 +126,11 @@ fun Application.configureRouting() {
val versions = DB.getLibraryVersions()
call.respond(versions)
}
get("/version/latest") {
get("/versions/latest") {
val versions = DB.getLibraryVersions()
call.respond(versions.first())
}
get("/version/{version}/download") {
get("/versions/{version}/download") {
val version = call.parameters["version"] ?: return@get call.respond(
HttpStatusCode.BadRequest,
"message" to "Invalid version."
@ -150,7 +152,7 @@ fun Application.configureRouting() {
val mrVersion = json.decodeFromString<ModrinthVersion>(mrVersionString)
call.respond(mrVersion.files[0])
}
get("/version/{version}") {
get("/versions/{version}") {
val version = call.parameters["version"] ?: return@get call.respond(
HttpStatusCode.BadRequest,
"message" to "Invalid version."

View file

@ -1,4 +1,4 @@
package dev.frogmc.meta.plugins
package dev.frogmc.plugins
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*

View file

@ -1,4 +1,4 @@
package dev.frogmc.meta.types
package dev.frogmc.types
import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable

View file

@ -1,4 +1,4 @@
package dev.frogmc.meta.types
package dev.frogmc.types
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.Table
@ -19,3 +19,9 @@ object LibraryVersions : Table() {
override val primaryKey = PrimaryKey(version)
}
object File : Table() {
val version = text("version")
val sha1 = text("sha1")
val size = text("size")
val url = text("url")
}

View file

@ -1,24 +1,14 @@
import ch.qos.logback.core.joran.spi.ConsoleTarget
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.FileAppender
def environment = System.getenv("ENVIRONMENT") ?: "production"
def defaultLevel = INFO
def defaultTarget = ConsoleTarget.SystemErr
appender("FILE", FileAppender) {
file = System.getenv("FROGMC_META_LOG") ?: "logs_meta.log"
append = true
encoder(PatternLayoutEncoder) {
pattern = "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n"
}
}
appender("CONSOLE", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%boldGreen(%d{yyyy-MM-dd}) %boldYellow(%d{HH:mm:ss}) %gray(|) %highlight(%5level) %gray(|) %boldMagenta(%40.40logger{40}) %gray(|) %msg%n"
pattern = "WAWA:: %boldGreen(%d{yyyy-MM-dd}) %boldYellow(%d{HH:mm:ss}) %gray(|) %highlight(%5level) %gray(|) %boldMagenta(%40.40logger{40}) %gray(|) %msg%n"
withJansi = true
}
@ -26,4 +16,4 @@ appender("CONSOLE", ConsoleAppender) {
target = defaultTarget
}
root(defaultLevel, ["CONSOLE", "FILE"])
root(defaultLevel, ["CONSOLE"])

View file

@ -17,7 +17,6 @@ importsAcceptList = [
'ch.qos.logback.core.BasicStatusManager',
'ch.qos.logback.core.ConsoleAppender',
'ch.qos.logback.core.FileAppender',
'ch.qos.logback.core.hook.ShutdownHook',
'ch.qos.logback.core.hook.ShutdownHookBase',
'ch.qos.logback.core.hook.DelayingShutdownHook',

View file

@ -18,28 +18,28 @@
</div>
<br>
<div class="route">
<code>GET /v1/loader/version/latest</code>
<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/version/{version}</code>
<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/version/{version}/download</code>
<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/version/upload</code>
<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/version/delete/{version}</code>
<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>
@ -50,17 +50,17 @@
</div>
<br>
<div class="route">
<code>GET /v1/library/version/latest</code>
<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/version/{version}</code>
<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/version/{version}/download</code>
<code>GET /v1/library/versions/{version}/download</code>
<p>Fetches the download URL of a specific version of FrogMC library.</p>
</div>
</div>