Skip to content

Commit

Permalink
The /services/{service} endpoint now outputs in Json, Added /tasks en…
Browse files Browse the repository at this point in the history
…dpoint
  • Loading branch information
TheCGuyGitHub committed Jul 7, 2024
1 parent ad4f7ec commit b9ebbfc
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}!")
}
Expand All @@ -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) {
Expand Down Expand Up @@ -150,7 +153,7 @@ class CloudNet_Rest_Module : DriverModule() {
}
}


//services
get("/services") {
val services = jsonUtils.services(cloudServiceManager)
val tokens = dbm.tokens()
Expand All @@ -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!")
}
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b9ebbfc

Please sign in to comment.