From 19dd601acd06f03de4a494f4e57a163cfebc55ab Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Fri, 29 Mar 2024 00:15:34 +0800 Subject: [PATCH] feat: Optimize navigation (#384) --- .../rest/dashboard/DashboardConfiguration.kt | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/cosky-rest-api/src/main/kotlin/me/ahoo/cosky/rest/dashboard/DashboardConfiguration.kt b/cosky-rest-api/src/main/kotlin/me/ahoo/cosky/rest/dashboard/DashboardConfiguration.kt index 74d0980a..0ba62abe 100644 --- a/cosky-rest-api/src/main/kotlin/me/ahoo/cosky/rest/dashboard/DashboardConfiguration.kt +++ b/cosky-rest-api/src/main/kotlin/me/ahoo/cosky/rest/dashboard/DashboardConfiguration.kt @@ -13,12 +13,11 @@ package me.ahoo.cosky.rest.dashboard import me.ahoo.cosky.rest.support.RequestPathPrefix -import org.springframework.http.HttpStatus -import org.springframework.http.server.reactive.ServerHttpResponse +import org.springframework.boot.autoconfigure.web.WebProperties +import org.springframework.http.ResponseEntity import org.springframework.stereotype.Controller +import org.springframework.util.ResourceUtils import org.springframework.web.bind.annotation.GetMapping -import reactor.core.publisher.Mono -import java.net.URI /** * for Dashboard-UI. @@ -26,7 +25,15 @@ import java.net.URI * @author ahoo wang */ @Controller -class DashboardConfiguration { +class DashboardConfiguration(private val webProperties: WebProperties) { + private val homePageContent by lazy { + val indexFilePath = webProperties.resources.staticLocations.first() + HOME_FILE + val indexFile = ResourceUtils.getFile(indexFilePath) + check(indexFile.exists()) { "$HOME_FILE not found in ${indexFile.absolutePath}" } + indexFile.readBytes() + } + + @Suppress("SpreadOperator") @GetMapping( *[ "/", @@ -42,14 +49,13 @@ class DashboardConfiguration { "${RequestPathPrefix.DASHBOARD}login", ], ) - fun home(response: ServerHttpResponse): Mono { - response.statusCode = HttpStatus.TEMPORARY_REDIRECT - response.headers.location = INDEX_PAGE_URI - return response.setComplete() + fun home(): ResponseEntity { + return ResponseEntity.ok() + .contentType(org.springframework.http.MediaType.TEXT_HTML) + .body(homePageContent) } companion object { - const val INDEX_PAGE = RequestPathPrefix.DASHBOARD + "index.html" - val INDEX_PAGE_URI = URI.create(INDEX_PAGE) + const val HOME_FILE = "index.html" } }