Skip to content

Commit

Permalink
feat: Customize port via config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawsson committed Jan 20, 2025
1 parent abfbb07 commit 6210a89
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 61 deletions.
125 changes: 67 additions & 58 deletions common/src/main/kotlin/host/carbon/common/KtorManager.kt
Original file line number Diff line number Diff line change
@@ -1,88 +1,97 @@
package host.carbon.common

import host.carbon.common.types.*
import host.carbon.common.types.players.PlayerCountInfo
import host.carbon.common.types.PaginatedResponse
import host.carbon.common.types.Pagination
import host.carbon.common.types.ServerInfo
import host.carbon.common.types.ServerResourceInfo
import host.carbon.common.types.players.PlayersInfo
import io.ktor.serialization.gson.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

class KtorManager(private val carbonAPI: CarbonAPI) {

fun startServer() {
embeddedServer(
Netty,
port = 6,
) {
install(CORS)
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
class KtorManager(private val carbonAPI: CarbonAPI, private val port: Int) {

private val server = embeddedServer(
Netty,
port = port,
) {
install(CORS)
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}

routing {
get("/") {
call.respondText("Hello World!")
}
routing {
get("/") {
call.respondText("Hello World!")
}

route("/v1") {
get {
val info = ServerInfo(
carbonAPI.getTPS(),
carbonAPI.getMSPT(),
ServerResourceInfo(
carbonAPI.getMemoryUsage(),
carbonAPI.getTotalMemory(),
carbonAPI.getCPUUsage(),
carbonAPI.getCPUCores()
),
carbonAPI.getPlayerCountInfo(),
)
route("/v1") {
get {
val info = ServerInfo(
carbonAPI.getTPS(),
carbonAPI.getMSPT(),
ServerResourceInfo(
carbonAPI.getMemoryUsage(),
carbonAPI.getTotalMemory(),
carbonAPI.getCPUUsage(),
carbonAPI.getCPUCores()
),
carbonAPI.getPlayerCountInfo(),
)

call.respond(info)
}
call.respond(info)
}

get("/players") {
val limit = call.request.queryParameters["limit"]?.toInt() ?: 25
val offset = call.request.queryParameters["offset"]?.toInt() ?: 0
get("/players") {
val limit = call.request.queryParameters["limit"]?.toInt() ?: 25
val offset = call.request.queryParameters["offset"]?.toInt() ?: 0

val players = carbonAPI.getOnlinePlayers(limit.coerceAtMost(100), offset)
val players = carbonAPI.getOnlinePlayers(limit.coerceAtMost(100), offset)

call.respond(
PaginatedResponse(
Pagination(limit, offset),
PlayersInfo(
players,
carbonAPI.getPlayerCountInfo()
)
call.respond(
PaginatedResponse(
Pagination(limit, offset),
PlayersInfo(
players,
carbonAPI.getPlayerCountInfo()
)
)
}
)
}

// TODO: Implement pagination
get("/commands") {
val limit = call.request.queryParameters["limit"]?.toInt() ?: 250
val offset = call.request.queryParameters["offset"]?.toInt() ?: 0
val query = call.request.queryParameters["query"]
// TODO: Implement pagination
get("/commands") {
val limit = call.request.queryParameters["limit"]?.toInt() ?: 250
val offset = call.request.queryParameters["offset"]?.toInt() ?: 0
val query = call.request.queryParameters["query"]

val commands = carbonAPI.getCommands(query)
val commands = carbonAPI.getCommands(query)

call.respond(
PaginatedResponse(
Pagination(limit, offset),
commands
)
call.respond(
PaginatedResponse(
Pagination(limit, offset),
commands
)
}
)
}
}
}.start(wait = false)
}
}

fun startServer() {
server.start(wait = false)
}

fun stopServer() {
server.stop()
}

}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
projectVersion = 0.0.1
projectVersion = 0.0.2

# Optimisations! - Disable if having issues with builds.
org.gradle.daemon=true
Expand Down
18 changes: 16 additions & 2 deletions spigot/src/main/kotlin/host/carbon/plugin/spigot/CarbonPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ import org.bukkit.plugin.java.JavaPlugin


class CarbonPlugin : JavaPlugin() {
private lateinit var ktorManager: KtorManager

override fun onEnable() {
KtorManager(CarbonPluginAPI()).startServer()
saveConfig()

val port = config.getInt("port")

ktorManager = KtorManager(CarbonPluginAPI(), port)
ktorManager.startServer()

logger.info("Carbon API started on port $port")
}

override fun onDisable() {}
override fun onDisable() {
if (::ktorManager.isInitialized) {
ktorManager.stopServer()
logger.info("Carbon API stopped")
}
}
}
1 change: 1 addition & 0 deletions spigot/src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
port:

0 comments on commit 6210a89

Please sign in to comment.