Skip to content

Commit

Permalink
feat: add a endpoint to provide system overview
Browse files Browse the repository at this point in the history
- add new system route to handle system overview requests
- implement system controller to manage system overview logic
- create system service and service implementation for data retrieval
- define new data classes for VO representations of system overview
- update instance center to include system service initialization
  • Loading branch information
ShiinaKin committed Nov 18, 2024
1 parent f3272ed commit 2daa5ed
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/src/main/kotlin/io/sakurasou/config/RoutingCenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import io.ktor.server.routing.*
import io.sakurasou.controller.*
import io.sakurasou.di.InstanceCenter.albumService
import io.sakurasou.di.InstanceCenter.authService
import io.sakurasou.di.InstanceCenter.commonService
import io.sakurasou.di.InstanceCenter.groupService
import io.sakurasou.di.InstanceCenter.imageService
import io.sakurasou.di.InstanceCenter.personalAccessTokenService
import io.sakurasou.di.InstanceCenter.roleService
import io.sakurasou.di.InstanceCenter.settingService
import io.sakurasou.di.InstanceCenter.strategyService
import io.sakurasou.di.InstanceCenter.systemService
import io.sakurasou.di.InstanceCenter.userService
import io.sakurasou.plugins.SiteInitCheckPlugin

Expand All @@ -39,6 +39,7 @@ fun Route.apiRoute() {
groupRoute(groupService)
roleRoute(roleService)
personalAccessTokenRoute(personalAccessTokenService)
systemRoute(systemService)
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/kotlin/io/sakurasou/controller/SystemController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.sakurasou.controller

import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.http.*
import io.ktor.server.routing.*
import io.sakurasou.constant.SETTING_READ
import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.SystemOverviewVO
import io.sakurasou.extension.success
import io.sakurasou.plugins.AuthorizationPlugin
import io.sakurasou.service.system.SystemService

/**
* @author Shiina Kin
* 2024/11/18 20:12
*/

fun Route.systemRoute(systemService: SystemService) {
val controller = SystemController(systemService)
route("system", {
tags("system")
protected = true
}) {
install(AuthorizationPlugin) {
permission = SETTING_READ
}
systemOverview(controller)
}
}

private fun Route.systemOverview(controller: SystemController) {
route {
get("overview", {
response {
HttpStatusCode.OK to {
body<CommonResponse<SystemOverviewVO>> {
description = "system overview"
}
}
}
}) {
val systemOverviewVO = controller.handleSystemOverview()
call.success(systemOverviewVO)
}
}
}

class SystemController(
private val systemService: SystemService
) {
suspend fun handleSystemOverview(): SystemOverviewVO {
val systemOverviewVO = systemService.fetchSystemOverview()
return systemOverviewVO
}
}
25 changes: 25 additions & 0 deletions app/src/main/kotlin/io/sakurasou/controller/vo/SettingVO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,29 @@ import kotlinx.serialization.Serializable
data class SettingVO(
val name: String,
val config: SettingConfig
)

@Serializable
@Name("SystemOverviewVO")
data class SystemOverviewVO(
val hoshizoraStatus: HoshizoraStatusVO,
val systemStatus: SystemStatusVO
)

@Serializable
@Name("HoshizoraStatusVO")
data class HoshizoraStatusVO(
val version: String,
val buildTime: String,
val commitId: String
)

@Serializable
@Name("SystemStatusVO")
data class SystemStatusVO(
val javaVersion: String,
val databaseVersion: String,
val operatingSystem: String,
val serverTimeZone: String,
val serverLanguage: String
)
4 changes: 4 additions & 0 deletions app/src/main/kotlin/io/sakurasou/di/InstanceCenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import io.sakurasou.service.setting.SettingService
import io.sakurasou.service.setting.SettingServiceImpl
import io.sakurasou.service.strategy.StrategyService
import io.sakurasou.service.strategy.StrategyServiceImpl
import io.sakurasou.service.system.SystemService
import io.sakurasou.service.system.SystemServiceImpl
import io.sakurasou.service.user.UserService
import io.sakurasou.service.user.UserServiceImpl
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -79,6 +81,7 @@ object InstanceCenter {
lateinit var roleService: RoleService
lateinit var personalAccessTokenService: PersonalAccessTokenService

lateinit var systemService: SystemService
lateinit var systemStatus: SystemStatus
lateinit var rolePermissions: Map<String, Set<String>>

Expand All @@ -105,6 +108,7 @@ object InstanceCenter {
}

fun initService() {
systemService = SystemServiceImpl()
settingService = SettingServiceImpl(settingDao)
strategyService = StrategyServiceImpl(strategyDao)

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/kotlin/io/sakurasou/service/system/SystemService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.sakurasou.service.system

import io.sakurasou.controller.vo.SystemOverviewVO

/**
* @author Shiina Kin
* 2024/11/18 20:17
*/
interface SystemService {
suspend fun fetchSystemOverview(): SystemOverviewVO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.sakurasou.service.system

import io.sakurasou.controller.vo.HoshizoraStatusVO
import io.sakurasou.controller.vo.SystemOverviewVO
import io.sakurasou.controller.vo.SystemStatusVO
import io.sakurasou.model.DatabaseSingleton.dbQuery
import kotlinx.datetime.TimeZone
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.util.*

/**
* @author Shiina Kin
* 2024/11/18 20:18
*/
class SystemServiceImpl: SystemService {
override suspend fun fetchSystemOverview(): SystemOverviewVO {
val buildRecord = Properties().also {
it.load(this::class.java.classLoader.getResourceAsStream("buildRecord.properties"))
}

val hoshizoraStatus = HoshizoraStatusVO(
buildTime = buildRecord["buildTime"] as String,
commitId = buildRecord["commitId"] as String,
version = buildRecord["version"] as String
)

val javaVersion = System.getProperty("java.version")
val databaseVersion = dbQuery {
TransactionManager.current().exec("SELECT VERSION()") { resultSet ->
resultSet.next()
resultSet.getString(1)
}
}!!
val osName = System.getProperty("os.name")
val osVersion = System.getProperty("os.version")
val serverTimeZone = TimeZone.currentSystemDefault().id
val serverLanguage = Locale.getDefault().language

val systemStatusVO = SystemStatusVO(
javaVersion = javaVersion,
databaseVersion = databaseVersion,
operatingSystem = "$osName $osVersion",
serverTimeZone = serverTimeZone,
serverLanguage = serverLanguage
)

return SystemOverviewVO(
hoshizoraStatus = hoshizoraStatus,
systemStatus = systemStatusVO
)
}
}

0 comments on commit 2daa5ed

Please sign in to comment.