From b9ebbfcb926a211e336616f0be9116f9eb993068 Mon Sep 17 00:00:00 2001 From: TheCGuyGitHub Date: Sun, 7 Jul 2024 11:34:33 +0200 Subject: [PATCH] The /services/{service} endpoint now outputs in Json, Added /tasks endpoint --- .../CloudNet_Rest_Module.kt | 29 ++++-- .../cloudnet_rest_module/utli/JsonUtils.kt | 90 +++++++++++++++++++ 2 files changed, 112 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/CloudNet_Rest_Module.kt b/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/CloudNet_Rest_Module.kt index 7725bec..185cb04 100644 --- a/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/CloudNet_Rest_Module.kt +++ b/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/CloudNet_Rest_Module.kt @@ -13,6 +13,7 @@ import eu.cloudnetservice.driver.module.driver.DriverModule import eu.cloudnetservice.node.ShutdownHandler import eu.cloudnetservice.node.command.CommandProvider import eu.cloudnetservice.node.service.CloudServiceManager +import eu.cloudnetservice.driver.provider.ServiceTaskProvider import io.github.thecguy.cloudnet_rest_module.commands.rest import io.github.thecguy.cloudnet_rest_module.config.Configuration import io.github.thecguy.cloudnet_rest_module.coroutines.AuthChecker @@ -85,13 +86,14 @@ class CloudNet_Rest_Module : DriverModule() { fun started( @NotNull cloudServiceManager: CloudServiceManager, @NotNull shutdownHandler: ShutdownHandler, + @NotNull serviceTaskProvider: ServiceTaskProvider, @NotNull @Named("module") injectionLayer: InjectionLayer<*> ) { I18n.loadFromLangPath(CloudNet_Rest_Module::class.java) GlobalScope.launch { - main(cloudServiceManager, shutdownHandler) + main(cloudServiceManager, shutdownHandler, serviceTaskProvider) } println("Rest API listening on port {configuration!!.restapi_port}!") } @@ -108,7 +110,8 @@ class CloudNet_Rest_Module : DriverModule() { private fun main( @NotNull cloudServiceManager: CloudServiceManager, - @NotNull shutdownHandler: ShutdownHandler + @NotNull shutdownHandler: ShutdownHandler, + @NotNull serviceTaskProvider: ServiceTaskProvider ) { val port = configuration!!.restapi_port embeddedServer(Netty, port = port) { @@ -150,7 +153,7 @@ class CloudNet_Rest_Module : DriverModule() { } } - + //services get("/services") { val services = jsonUtils.services(cloudServiceManager) val tokens = dbm.tokens() @@ -173,10 +176,7 @@ class CloudNet_Rest_Module : DriverModule() { val rToken = call.request.headers["Authorization"] if (tokens.contains(rToken)) { if (serv) { - val servout = cloudServiceManager.serviceByName(call.parameters["service"].toString()) - if (servout != null) { - call.respondText("Service: ${servout.name()} \n ServiceID: ${servout.serviceId()} \n Address: ${servout.address()} \n Connected: ${servout.connected()} \n ConnectedTime: ${servout.connectedTime()} \n CreationTime: ${servout.creationTime()} \n LifeCycle: ${servout.lifeCycle()} \n Provider: ${servout.provider()}") - } + call.respond(jsonUtils.service(cloudServiceManager, call.parameters["service"].toString()).toString(4)) } else { call.respondText("Du Mensch, es gibt diesen Service NICHT! Wasn vollidiot!") } @@ -185,6 +185,21 @@ class CloudNet_Rest_Module : DriverModule() { } } + //tasks + get("/tasks") { + val tokens = dbm.tokens() + val rToken = call.request.headers["Authorization"] + if (tokens.contains(rToken)) { + call.respond(jsonUtils.tasks(serviceTaskProvider).toString(4)) + } else { + call.response.status(HttpStatusCode.Unauthorized) + } + } + + + + + get("/node/shutdown") { call.respondText("NOTE: The Cloud is shutting down!") shutdownHandler.shutdown() diff --git a/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/utli/JsonUtils.kt b/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/utli/JsonUtils.kt index ee1e6ff..28d70bd 100644 --- a/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/utli/JsonUtils.kt +++ b/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/utli/JsonUtils.kt @@ -1,8 +1,10 @@ package io.github.thecguy.cloudnet_rest_module.utli +import eu.cloudnetservice.driver.provider.ServiceTaskProvider import eu.cloudnetservice.node.service.CloudServiceManager import kong.unirest.core.json.JSONArray import kong.unirest.core.json.JSONObject +import org.jetbrains.annotations.NotNull import java.util.* @@ -34,6 +36,94 @@ class JsonUtils internal constructor() { result.put("services", servicesArray) return result } + + fun service(cloudServiceManager: CloudServiceManager, @NotNull service: String): JSONObject { + val ser = cloudServiceManager.serviceByName(service) + val serv = JSONObject() + if (ser != null) { + serv.put("name", ser.name()) + println("1") + val addressObject = JSONObject() + addressObject.put("host", ser.address().host) + addressObject.put("port", ser.address().port) + serv.put("address", addressObject) + println("2") + serv.put("connected", ser.connected()) + serv.put("connectedTime", ser.connectedTime()) + serv.put("lifeCycle", ser.lifeCycle()) + serv.put("creationTime", ser.creationTime()) + println("3") + val configuration = JSONObject() + configuration.put("staticService", ser.configuration().staticService()) + configuration.put("autoDeleteOnStop", ser.configuration().autoDeleteOnStop()) + configuration.put("groups", ser.configuration().groups()) + configuration.put("runtime", ser.configuration().runtime()) + serv.put("configuration", configuration) + println("4") + val processConfig = JSONObject() + processConfig.put("jvmOptions", ser.configuration().processConfig().jvmOptions) + processConfig.put("environment", ser.configuration().processConfig().environment) + processConfig.put("maxHeapMemorySize", ser.configuration().processConfig().maxHeapMemorySize) + processConfig.put("processParameters", ser.configuration().processConfig().processParameters) + processConfig.put("environmentVariables", ser.configuration().processConfig().environmentVariables) + serv.put("processConfig", processConfig) + println("5") + val processSnapshot = JSONObject() + processSnapshot.put("pid", ser.processSnapshot().pid) + processSnapshot.put("threads", ser.processSnapshot().threads) + processSnapshot.put("cpuUsage", ser.processSnapshot().cpuUsage) + processSnapshot.put("maxHeapMemory", ser.processSnapshot().maxHeapMemory) + processSnapshot.put("currentLoadedClassCount", ser.processSnapshot().currentLoadedClassCount) + processSnapshot.put("heapUsageMemory", ser.processSnapshot().heapUsageMemory) + processSnapshot.put("noHeapUsageMemory", ser.processSnapshot().noHeapUsageMemory) + processSnapshot.put("systemCpuUsage", ser.processSnapshot().systemCpuUsage) + processSnapshot.put("unloadedClassCount", ser.processSnapshot().unloadedClassCount) + serv.put("processSnapshot", processSnapshot) + println("6") + } + println("7") + println(serv.toString(4)) + + return serv + } + + fun tasks(serviceTaskProvider: ServiceTaskProvider):JSONObject { + val taskT = serviceTaskProvider.serviceTasks() + val tasksArray = JSONArray() + taskT.forEach { task -> + val jTask = JSONObject() + jTask.put("name", task.name()) + jTask.put("groups", task.groups()) + jTask.put("runtime", task.runtime()) + jTask.put("autoDeleteOnStop", task.autoDeleteOnStop()) + jTask.put("jvmOptions", task.jvmOptions()) + jTask.put("associatedNodes", task.associatedNodes()) + jTask.put("hostAddress", task.hostAddress()) + jTask.put("javaCommand", task.javaCommand()) + jTask.put("maintenance", task.maintenance()) + jTask.put("minServiceCount", task.minServiceCount()) + jTask.put("nameSplitter", task.nameSplitter()) + jTask.put("startPort", task.startPort()) + jTask.put("staticServices", task.staticServices()) + + val processConfiguration = JSONObject() + processConfiguration.put("environment", task.processConfiguration().environment) + processConfiguration.put("jvmOptions", task.processConfiguration().jvmOptions) + processConfiguration.put("processParameters", task.processConfiguration().processParameters) + processConfiguration.put("maxHeapMemorySize", task.processConfiguration().maxHeapMemorySize) + processConfiguration.put("environmentVariables", task.processConfiguration().environmentVariables) + jTask.put("processConfiguration", processConfiguration) + + + tasksArray.put(jTask) + } + val result = JSONObject() + result.put("tasks", tasksArray) + return result + } + + + fun token():JSONObject { val token = JSONObject() val ttoken = authUtil.generateToken(256)