From 30e0b24a4326cdb27d6c9879731636611a8d5129 Mon Sep 17 00:00:00 2001 From: TheCGuyGitHub Date: Sun, 30 Jun 2024 21:12:20 +0200 Subject: [PATCH] Added option to change the RestAPI server port --- .../CloudNet_Rest_Module.kt | 40 ++++++++++--------- .../cloudnet_rest_module/config/config.kt | 3 +- 2 files changed, 24 insertions(+), 19 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 f42440c..4ee24b4 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 @@ -27,6 +27,7 @@ import jakarta.inject.Named import jakarta.inject.Singleton import kong.unirest.core.json.JSONArray import kong.unirest.core.json.JSONObject +import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.jetbrains.annotations.NotNull @@ -42,21 +43,6 @@ class CloudNet_Rest_Module : DriverModule() { - - - - @ModuleTask(lifecycle = ModuleLifeCycle.STARTED) - fun started( - @NotNull cloudServiceManager: CloudServiceManager, - @NotNull shutdownHandler: ShutdownHandler, - @NotNull @Named("module") injectionLayer: InjectionLayer<*> - ) { - logger.info("Listening on port 8080!") - GlobalScope.launch { - main(cloudServiceManager, shutdownHandler) - } - } - @ModuleTask(order = 127, lifecycle = ModuleLifeCycle.LOADED) fun load() { val config = this.readConfig(DocumentFactory.json()) @@ -67,7 +53,8 @@ class CloudNet_Rest_Module : DriverModule() { config.getString("password"), config.getString("database"), config.getString("host"), - config.getInt("port") + config.getInt("port"), + config.getInt("restapi_port") ) ) ) @@ -82,12 +69,16 @@ class CloudNet_Rest_Module : DriverModule() { "123456", "cloudnet_rest", "127.0.0.1", - 3306 + 3306, + 8080 ) }, DocumentFactory.json() ) + + + val config = HikariConfig() config.jdbcUrl = "jdbc:mysql://${configuration!!.host}:${configuration!!.port}/${configuration!!.database}" @@ -114,6 +105,18 @@ class CloudNet_Rest_Module : DriverModule() { ds.close() } + @ModuleTask(order = 127, lifecycle = ModuleLifeCycle.STARTED) + fun started( + @NotNull cloudServiceManager: CloudServiceManager, + @NotNull shutdownHandler: ShutdownHandler, + @NotNull @Named("module") injectionLayer: InjectionLayer<*> + ) { + GlobalScope.launch { + main(cloudServiceManager, shutdownHandler) + } + logger.info("Rest API listening on port ${configuration!!.restapi_port.toString()}!") + } + @ModuleTask(lifecycle = ModuleLifeCycle.STARTED) fun start(commandProvider: CommandProvider) { commandProvider.register(Test::class.java) @@ -145,7 +148,8 @@ class CloudNet_Rest_Module : DriverModule() { @NotNull cloudServiceManager: CloudServiceManager, @NotNull shutdownHandler: ShutdownHandler ) { - embeddedServer(Netty, port = 8080) { + val port = configuration!!.restapi_port + embeddedServer(Netty, port = port) { install(ShutDownUrl.ApplicationCallPlugin) { shutDownUrl = "/debug/shutdown" diff --git a/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/config/config.kt b/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/config/config.kt index 51ba861..51b684d 100644 --- a/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/config/config.kt +++ b/src/main/kotlin/io/github/thecguy/cloudnet_rest_module/config/config.kt @@ -8,5 +8,6 @@ class Configuration( @NotNull val password: String, @NotNull val database: String, @NotNull val host: String, - @NotNull val port: Int + @NotNull val port: Int, + @NotNull val restapi_port: Int ) \ No newline at end of file