All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.neko233.virtualRouter.server.config.RouterServerConfig.kt Maven / Gradle / Ivy

package com.neko233.virtualRouter.server.config

import com.neko233.virtualRouter.core.utils.FileUtilsForRouter
import com.neko233.virtualRouter.core.utils.JsonUtils
import com.neko233.virtualRouter.core.utils.ResourcesJdkUtilsForRouter
import org.slf4j.LoggerFactory
import java.io.File

/**
 * Router Client Config =
 * 1. Router Client 要连接的 route server ip:port
 * 1. 本机 RPC Server 的 ip:port | 用于提供给其他 Route Node 进行调用
 *
 * @author SolarisNeko
 * Date on 2024-01-05
 * */
class RouterServerConfig {

    // 路由服务器的对外端口
    var routerServerPort: Int = 0

    companion object {
        @JvmStatic
        private val LOGGER = LoggerFactory.getLogger(RouterServerConfig::class.java)!!

        /**
         *  读取本地配置
         */
        @JvmStatic
        fun readFromConfigFile(
            configName: String,
        ): RouterServerConfig {
            val configFile: File = ResourcesJdkUtilsForRouter.getResourceFile(
                configName
            ) ?: throw RuntimeException("config file not found. configName = ${configName}")

            if (!configFile.exists()) {
                val newJson = JsonUtils.serialize(RouterServerConfig())
                FileUtilsForRouter.write(configFile, newJson)

                throw RuntimeException("没有在当前路径找到配置文件, 自动给你生成了一个 ${configName}, 配置好后再启动项目!")
            }
            val configJson = FileUtilsForRouter.readAllContent(configFile)
            val info = JsonUtils.deserialize(configJson, RouterServerConfig::class.java)
            info!!


            if (info.routerServerPort == 0) {
                throw RuntimeException("请再检查一下 ${configName} 的配置, json = ${JsonUtils.serialize(info)}")
            }

            return info
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy