-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
61 deletions.
There are no files selected for viewing
125 changes: 67 additions & 58 deletions
125
common/src/main/kotlin/host/carbon/common/KtorManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
port: |