mirror of
https://github.com/revanced/revanced-api.git
synced 2025-04-29 22:24:31 +02:00
feat: Add CLI
This commit is contained in:
parent
9999b242ad
commit
a988ffbd23
@ -7,12 +7,18 @@ plugins {
|
||||
group = "app.revanced"
|
||||
|
||||
application {
|
||||
mainClass.set("app.revanced.api.ApplicationKt")
|
||||
mainClass.set("app.revanced.api.command.MainCommandKt")
|
||||
|
||||
val isDevelopment: Boolean = project.ext.has("development")
|
||||
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
|
||||
}
|
||||
|
||||
tasks {
|
||||
processResources {
|
||||
expand("projectVersion" to project.version)
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
@ -44,6 +50,7 @@ dependencies {
|
||||
implementation(libs.dotenv.kotlin)
|
||||
implementation(libs.ktoml.core)
|
||||
implementation(libs.ktoml.file)
|
||||
implementation(libs.picocli)
|
||||
|
||||
testImplementation(libs.ktor.server.tests)
|
||||
testImplementation(libs.kotlin.test.junit)
|
||||
|
@ -7,6 +7,7 @@ koin="3.5.3"
|
||||
dotenv="6.4.1"
|
||||
ktor = "2.3.7"
|
||||
ktoml = "0.5.1"
|
||||
picocli = "4.7.3"
|
||||
|
||||
[libraries]
|
||||
ktor-client-core = { module = "io.ktor:ktor-client-core" }
|
||||
@ -37,6 +38,7 @@ ktor-server-tests = { module = "io.ktor:ktor-server-tests" }
|
||||
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
|
||||
ktoml-core = { module = "com.akuleshov7:ktoml-core", version.ref = "ktoml" }
|
||||
ktoml-file = { module = "com.akuleshov7:ktoml-file", version.ref = "ktoml" }
|
||||
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
|
||||
|
||||
[plugins]
|
||||
serilization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
|
||||
|
@ -1,22 +0,0 @@
|
||||
package app.revanced.api
|
||||
|
||||
import app.revanced.api.plugins.*
|
||||
import io.github.cdimascio.dotenv.Dotenv
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.netty.*
|
||||
|
||||
fun main() {
|
||||
embeddedServer(Netty, port = 8080, host = "0.0.0.0", configure = {
|
||||
connectionGroupSize = 1
|
||||
workerGroupSize = 1
|
||||
callGroupSize = 1
|
||||
}) {
|
||||
configureHTTP()
|
||||
configureSerialization()
|
||||
configureDatabases()
|
||||
configureSecurity()
|
||||
configureDependencies()
|
||||
configureRouting()
|
||||
}.start(wait = true)
|
||||
}
|
34
src/main/kotlin/app/revanced/api/command/MainCommand.kt
Normal file
34
src/main/kotlin/app/revanced/api/command/MainCommand.kt
Normal file
@ -0,0 +1,34 @@
|
||||
package app.revanced.api.command
|
||||
|
||||
import picocli.CommandLine
|
||||
import java.util.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
CommandLine(MainCommand).execute(*args).let(System::exit)
|
||||
}
|
||||
|
||||
private object CLIVersionProvider : CommandLine.IVersionProvider {
|
||||
override fun getVersion() =
|
||||
arrayOf(
|
||||
MainCommand::class.java.getResourceAsStream(
|
||||
"/app/revanced/api/version.properties",
|
||||
)?.use { stream ->
|
||||
Properties().apply {
|
||||
load(stream)
|
||||
}.let {
|
||||
"ReVanced API v${it.getProperty("version")}"
|
||||
}
|
||||
} ?: "ReVanced API",
|
||||
)
|
||||
}
|
||||
|
||||
@CommandLine.Command(
|
||||
name = "revanced-api",
|
||||
description = ["API server for ReVanced"],
|
||||
mixinStandardHelpOptions = true,
|
||||
versionProvider = CLIVersionProvider::class,
|
||||
subcommands = [
|
||||
StartAPICommand::class,
|
||||
],
|
||||
)
|
||||
private object MainCommand
|
39
src/main/kotlin/app/revanced/api/command/StartAPICommand.kt
Normal file
39
src/main/kotlin/app/revanced/api/command/StartAPICommand.kt
Normal file
@ -0,0 +1,39 @@
|
||||
package app.revanced.api.command
|
||||
|
||||
import app.revanced.api.plugins.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.netty.*
|
||||
import picocli.CommandLine
|
||||
|
||||
@CommandLine.Command(
|
||||
name = "start",
|
||||
description = ["Start the API server"],
|
||||
)
|
||||
internal object StartAPICommand : Runnable {
|
||||
@CommandLine.Option(
|
||||
names = ["-h", "--host"],
|
||||
description = ["The host address to bind to."],
|
||||
)
|
||||
private var host: String = "0.0.0.0"
|
||||
|
||||
@CommandLine.Option(
|
||||
names = ["-p", "--port"],
|
||||
description = ["The port to listen on."],
|
||||
)
|
||||
private var port: Int = 8080
|
||||
|
||||
override fun run() {
|
||||
embeddedServer(Netty, port, host, configure = {
|
||||
connectionGroupSize = 1
|
||||
workerGroupSize = 1
|
||||
callGroupSize = 1
|
||||
}) {
|
||||
configureHTTP()
|
||||
configureSerialization()
|
||||
configureDatabases()
|
||||
configureSecurity()
|
||||
configureDependencies()
|
||||
configureRouting()
|
||||
}.start(wait = true)
|
||||
}
|
||||
}
|
@ -1,16 +1,12 @@
|
||||
package app.revanced.api.plugins
|
||||
|
||||
import app.revanced.api.APIConfiguration
|
||||
import app.revanced.api.schema.APIConfiguration
|
||||
import app.revanced.api.backend.github.GitHubBackend
|
||||
import com.akuleshov7.ktoml.Toml
|
||||
import com.akuleshov7.ktoml.source.decodeFromStream
|
||||
import io.github.cdimascio.dotenv.Dotenv
|
||||
import io.ktor.server.application.*
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.dsl.module
|
||||
import org.koin.ktor.ext.inject
|
||||
import org.koin.ktor.plugin.Koin
|
||||
import java.io.File
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
package app.revanced.api.plugins
|
||||
|
||||
import app.revanced.api.*
|
||||
import app.revanced.api.backend.github.GitHubBackend
|
||||
import io.ktor.client.utils.EmptyContent.contentType
|
||||
import app.revanced.api.schema.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.http.content.*
|
||||
|
@ -1,4 +1,4 @@
|
||||
package app.revanced.api
|
||||
package app.revanced.api.schema
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@ -52,4 +52,22 @@ class APIAsset(
|
||||
@Serializable
|
||||
class APIReleaseVersion(
|
||||
val version: String
|
||||
)
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class APIAnnouncement(
|
||||
val id: Int,
|
||||
val author: APIUser?,
|
||||
val title: String,
|
||||
val content: APIAnnouncementContent,
|
||||
val channel: String,
|
||||
val createdAt: String,
|
||||
val archivedAt: String?,
|
||||
val level: Int,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class APIAnnouncementContent(
|
||||
val message: String,
|
||||
val attachmentUrls: Set<String>
|
||||
)
|
@ -1,4 +1,4 @@
|
||||
package app.revanced.api
|
||||
package app.revanced.api.schema
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
1
src/main/resources/app.revanced.api/version.properties
Normal file
1
src/main/resources/app.revanced.api/version.properties
Normal file
@ -0,0 +1 @@
|
||||
version=${projectVersion}
|
Loading…
x
Reference in New Issue
Block a user