Skip to content

Commit

Permalink
some rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
Bumer-32 committed Jan 14, 2025
1 parent cbb67fd commit 07a617f
Show file tree
Hide file tree
Showing 24 changed files with 126 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import ua.pp.lumivoid.iwtcms.ktor.api.websockets.WebSocket
import ua.pp.lumivoid.iwtcms.ktor.api.websockets.WebSocketBaseInterface

object DevReloadWS: WebSocket() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
override var WSinterface: WebSocketBaseInterface? = null
override val PATH = "/dev/reloadWS"

override val ws: Routing.() -> Unit = {
logger.info("Initializing $PATH websocket")

webSocket(PATH) {
send(Frame.Text("Connected to $PATH"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@ import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import ua.pp.lumivoid.iwtcms.Constants

object ApiListGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object ApiListG: Request() {
override val PATH = "/apiList"

private val json = Json { prettyPrint = true }

private val apis = mutableMapOf<String, String>()

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("ApiListGET", PATH)

get(PATH) {
val response = json.encodeToString(apis)
call.respondText(response, contentType = ContentType.Text.Plain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.sessions.get
import io.ktor.server.sessions.sessions
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.ktor.cookie.UserSession
import ua.pp.lumivoid.iwtcms.util.Config

object CheckLoginGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object CheckLoginG: Request() {
override val PATH = "/api/checkLogin"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("CheckLoginGET", PATH)

get(PATH) {
val session = call.sessions.get<UserSession>()
if (session == null) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/ua/pp/lumivoid/iwtcms/ktor/api/requests/FilesG.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ua.pp.lumivoid.iwtcms.ktor.api.requests

import io.ktor.server.http.content.staticResources
import io.ktor.server.routing.Routing

object FilesG: Request() {
override val PATH = "/files"

override val request: Routing.() -> Unit = {
staticResources(PATH, "files")
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import io.ktor.http.ContentType
import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.util.Config

object IsAuthEnabledGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object IsAuthEnabledG: Request() {
override val PATH = "/api/isAuthEnabled"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("IsAuthEnabledGET", PATH)

get(PATH) {
call.respondText(Config.readConfig().useAuthentication.toString(), ContentType.Text.Plain)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import io.ktor.http.ContentType
import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.util.Config

object IsDevEnabledGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object IsDevEnabledG: Request() {
override val PATH = "/api/isDevEnabled"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("IsDevEnabledGET", PATH)

get(PATH) {
call.respondText(Config.readConfig().devMode.toString(), ContentType.Text.Plain)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@ import io.ktor.server.routing.post
import io.ktor.server.sessions.sessions
import io.ktor.server.sessions.set
import kotlinx.serialization.Serializable
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.User
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.ktor.cookie.UserSession
import ua.pp.lumivoid.iwtcms.util.Config

object LoginPOST: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object LoginP: Request() {
override val PATH = "/api/login"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")

registerAPI("LoginPOST", PATH)

post(PATH) {
val payload = call.receive<LoginPayload>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import io.ktor.server.routing.post
import io.ktor.server.sessions.clear
import io.ktor.server.sessions.get
import io.ktor.server.sessions.sessions
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.ktor.cookie.UserSession

object LogoutPOST: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object LogoutP: Request() {
override val PATH = "/api/logout"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")

registerAPI("LogoutPOST", PATH)

post(PATH) {
val session = call.sessions.get<UserSession>()
if (session == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ import io.ktor.server.routing.*
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.UserAuthentication
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI

object LogsHistoryGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object LogsHistoryG: Request() {
override val PATH = "/api/logsHistory"

private val json = Json { prettyPrint = true }
private val logs = mutableListOf<String>()

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("LogsHistoryGET", PATH)

get(PATH) {
UserAuthentication.doAuth(
call = call,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ua.pp.lumivoid.iwtcms.ktor.api.requests

import io.ktor.http.ContentType
import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import ua.pp.lumivoid.iwtcms.util.Config

object MCSettingsGP: Request() {
override val PATH = "/api/mcSettings"

override val request: Routing.() -> Unit = {
get(PATH) {
call.respondText(Config.readConfig().devMode.toString(), ContentType.Text.Plain)
}

post (PATH) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import io.ktor.server.routing.Routing
import kotlinx.coroutines.runBlocking
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.dev.KFSWFileWatcher
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.util.Config
import java.io.File

object MainGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object MainG: Request() {
override val PATH = "/"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("MainGET", PATH)
if (!Config.readConfig().devMode) {
if (Config.readConfig().enableIWTCMSControlPanel) {
staticResources(PATH, "web", index = "index.html")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@ import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.User
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.util.Config

object PermitsGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object PermitsG: Request() {
override val PATH = "/api/permits/{username}"

private val json = Json { prettyPrint = true }

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("PermitsGET", PATH)

get(PATH) {
val user: User? = Config.readConfig().users.find { it.username == call.parameters["username"] }
if (user != null) {
Expand Down
16 changes: 13 additions & 3 deletions src/main/kotlin/ua/pp/lumivoid/iwtcms/ktor/api/requests/Request.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package ua.pp.lumivoid.iwtcms.ktor.api.requests

import io.ktor.server.routing.Routing
import org.slf4j.Logger
import io.ktor.server.routing.RoutingRoot
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListG.registerAPI

abstract class Request {
protected abstract val logger: Logger
protected val logger = Constants.EMBEDDED_SERVER_LOGGER

@Suppress("PropertyName")
protected abstract val PATH: String
abstract val request: Routing.() -> Unit
protected abstract val request: Routing.() -> Unit

fun register(routing: RoutingRoot) {
logger.info("Initializing ${this.javaClass.simpleName} request: $PATH")
registerAPI(this.javaClass.simpleName, PATH)
request.invoke(routing)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI

object VersionGET: Request() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
object VersionG: Request() {
override val PATH = "/api/iwtcmsVersion"

override val request: Routing.() -> Unit = {
logger.info("Initializing $PATH request")
registerAPI("VersionGET", PATH)

get(PATH) {
call.respondText(Constants.MOD_VERSION, ContentType.Text.Plain)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.UserAuthentication
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListG.registerAPI
import ua.pp.lumivoid.iwtcms.util.MinecraftServerHandler

object ConsoleWS: WebSocket() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
override var WSinterface: WebSocketBaseInterface? = null
override val PATH = "/ws/console" // why console? because we use this socket same as console, receive logs and send commands

override val ws: Routing.() -> Unit = {
logger.info("Initializing $PATH websocket")
registerAPI("ConsoleWS", PATH)

webSocket(PATH) {
val status = UserAuthentication.doAuth(
call = call,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.UserAuthentication
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListGET.registerAPI
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListG.registerAPI
import ua.pp.lumivoid.iwtcms.util.Config
import ua.pp.lumivoid.iwtcms.util.ServerStats

object ServerStatsWS: WebSocket() {
override val logger = Constants.EMBEDDED_SERVER_LOGGER
override var WSinterface: WebSocketBaseInterface? = null
override val PATH = "/ws/serverStats"

private val json = Json { prettyPrint = true }

override val ws: Routing.() -> Unit = {
logger.info("Initializing $PATH websocket")
registerAPI("ServerStatsWS", PATH)

webSocket(PATH) {
val status = UserAuthentication.doAuth(
call = call,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package ua.pp.lumivoid.iwtcms.ktor.api.websockets

import io.ktor.server.routing.Routing
import org.slf4j.Logger
import io.ktor.server.routing.RoutingRoot
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.requests.ApiListG.registerAPI

abstract class WebSocket {
protected abstract val logger: Logger
protected val logger = Constants.EMBEDDED_SERVER_LOGGER

protected abstract var WSinterface: WebSocketBaseInterface?
protected abstract val PATH: String
abstract val ws: Routing.() -> Unit
abstract fun asWs(): WebSocketBaseInterface?

fun register(routing: RoutingRoot) {
logger.info("Initializing ${this.javaClass.simpleName} websocket: $PATH")
registerAPI(this.javaClass.simpleName, PATH)
ws.invoke(routing)
}
}
Loading

0 comments on commit 07a617f

Please sign in to comment.