From edc1223490972fe8f1b25492c853ae0f5802586f Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Tue, 16 Apr 2024 17:05:51 +0800 Subject: [PATCH] fix(komga-tray): open komga menu does not use custom port Closes: #1468 --- .../komga/application/gui/TrayIconRunner.kt | 10 +++++----- .../web/WebServerEffectiveSettings.kt | 19 +++++++++++++++++++ .../interfaces/api/rest/SettingsController.kt | 13 ++++--------- 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 komga/src/main/kotlin/org/gotson/komga/infrastructure/web/WebServerEffectiveSettings.kt diff --git a/komga-tray/src/main/kotlin/org/gotson/komga/application/gui/TrayIconRunner.kt b/komga-tray/src/main/kotlin/org/gotson/komga/application/gui/TrayIconRunner.kt index da76b7407..b0df7510f 100644 --- a/komga-tray/src/main/kotlin/org/gotson/komga/application/gui/TrayIconRunner.kt +++ b/komga-tray/src/main/kotlin/org/gotson/komga/application/gui/TrayIconRunner.kt @@ -5,6 +5,7 @@ import androidx.compose.ui.res.loadSvgPainter import androidx.compose.ui.window.Tray import androidx.compose.ui.window.application import org.gotson.komga.RB +import org.gotson.komga.infrastructure.web.WebServerEffectiveSettings import org.gotson.komga.openExplorer import org.gotson.komga.openUrl import org.springframework.beans.factory.annotation.Value @@ -20,15 +21,14 @@ import java.io.File @Profile("!test") @Component class TrayIconRunner( - @Value("#{servletContext.contextPath}") servletContextPath: String, @Value("#{komgaProperties.configDir}") komgaConfigDir: String, @Value("\${logging.file.name}") logFileName: String, - @Value("\${server.port}") serverPort: Int, + serverSettings: WebServerEffectiveSettings, env: Environment, ) : ApplicationRunner { - val komgaUrl = "http://localhost:$serverPort$servletContextPath" - val komgaConfigDir = File(komgaConfigDir) - val logFile = File(logFileName) + val komgaUrl by lazy { "http://localhost:${serverSettings.effectiveServerPort}${serverSettings.effectiveServletContextPath}" } + val komgaConfigDir by lazy { File(komgaConfigDir) } + val logFile by lazy { File(logFileName) } val iconFileName = if (env.activeProfiles.contains("mac")) "komga-gray-minimal.svg" else "komga-color.svg" @Async diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/WebServerEffectiveSettings.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/WebServerEffectiveSettings.kt new file mode 100644 index 000000000..71fd22723 --- /dev/null +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/web/WebServerEffectiveSettings.kt @@ -0,0 +1,19 @@ +package org.gotson.komga.infrastructure.web + +import jakarta.servlet.ServletContext +import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent +import org.springframework.context.event.EventListener +import org.springframework.stereotype.Component + +@Component +class WebServerEffectiveSettings( + servletContext: ServletContext, +) { + var effectiveServerPort: Int? = null + val effectiveServletContextPath: String = servletContext.contextPath + + @EventListener + fun onApplicationEvent(event: ServletWebServerInitializedEvent) { + effectiveServerPort = event.webServer.port + } +} diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/SettingsController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/SettingsController.kt index a7f35984e..d49422f65 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/SettingsController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/SettingsController.kt @@ -1,16 +1,15 @@ package org.gotson.komga.interfaces.api.rest -import jakarta.servlet.ServletContext import jakarta.validation.Valid import org.gotson.komga.domain.model.ROLE_ADMIN import org.gotson.komga.infrastructure.configuration.KomgaSettingsProvider +import org.gotson.komga.infrastructure.web.WebServerEffectiveSettings import org.gotson.komga.interfaces.api.rest.dto.SettingMultiSource import org.gotson.komga.interfaces.api.rest.dto.SettingsDto import org.gotson.komga.interfaces.api.rest.dto.SettingsUpdateDto import org.gotson.komga.interfaces.api.rest.dto.toDomain import org.gotson.komga.interfaces.api.rest.dto.toDto import org.springframework.beans.factory.annotation.Value -import org.springframework.boot.autoconfigure.web.ServerProperties import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.security.access.prepost.PreAuthorize @@ -29,12 +28,8 @@ class SettingsController( private val komgaSettingsProvider: KomgaSettingsProvider, @Value("\${server.port:#{null}}") private val configServerPort: Int?, @Value("\${server.servlet.context-path:#{null}}") private val configServerContextPath: String?, - serverProperties: ServerProperties, - servletContext: ServletContext, + private val serverSettings: WebServerEffectiveSettings, ) { - private val effectiveServerPort = serverProperties.port - private val effectiveServerContextPath = servletContext.contextPath - @GetMapping fun getSettings(): SettingsDto = SettingsDto( @@ -43,8 +38,8 @@ class SettingsController( komgaSettingsProvider.rememberMeDuration.inWholeDays, komgaSettingsProvider.thumbnailSize.toDto(), komgaSettingsProvider.taskPoolSize, - SettingMultiSource(configServerPort, komgaSettingsProvider.serverPort, effectiveServerPort), - SettingMultiSource(configServerContextPath, komgaSettingsProvider.serverContextPath, effectiveServerContextPath), + SettingMultiSource(configServerPort, komgaSettingsProvider.serverPort, serverSettings.effectiveServerPort), + SettingMultiSource(configServerContextPath, komgaSettingsProvider.serverContextPath, serverSettings.effectiveServletContextPath), ) @PatchMapping