mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-30 06:37:29 +00:00
refactor(config): 修改 AppConfig 的获取方式, 便于编写测试用例.
通过 Const 单例对象获取配置信息不利于编写测试用例, 所以改为利用参数的默认值来获取 Const 的 config 对象. Issue #5
This commit is contained in:
parent
f11290c73d
commit
d24572a4f3
@ -106,12 +106,12 @@ internal data class MavenRepositoryConfig(
|
|||||||
val authentication: Authentication? = null
|
val authentication: Authentication? = null
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun toRemoteRepository(): RemoteRepository {
|
fun toRemoteRepository(proxyConfig: ProxyConfig = Const.config.proxy): RemoteRepository {
|
||||||
val builder = RemoteRepository.Builder(null, checkRepositoryLayout(layout), url.toString())
|
val builder = RemoteRepository.Builder(null, checkRepositoryLayout(layout), url.toString())
|
||||||
if (proxy != null) {
|
if (proxy != null) {
|
||||||
builder.setProxy(proxy)
|
builder.setProxy(proxy)
|
||||||
} else if (Const.config.proxy.type == DefaultBotOptions.ProxyType.HTTP) {
|
} else if (proxyConfig.type == DefaultBotOptions.ProxyType.HTTP) {
|
||||||
builder.setProxy(Const.config.proxy.toAetherProxy())
|
builder.setProxy(proxyConfig.toAetherProxy())
|
||||||
}
|
}
|
||||||
return builder.build()
|
return builder.build()
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,7 @@ fun main(args: Array<String>): Unit = runBlocking {
|
|||||||
|
|
||||||
val launcher = Launcher()
|
val launcher = Launcher()
|
||||||
.registerShutdownHook()
|
.registerShutdownHook()
|
||||||
if (Const.config.metrics.enable) {
|
|
||||||
startMetricsServer()
|
startMetricsServer()
|
||||||
}
|
|
||||||
if (!launcher.launch()) {
|
if (!launcher.launch()) {
|
||||||
exitProcess(1)
|
exitProcess(1)
|
||||||
}
|
}
|
||||||
@ -34,11 +32,16 @@ fun main(args: Array<String>): Unit = runBlocking {
|
|||||||
* 启动运行指标服务器.
|
* 启动运行指标服务器.
|
||||||
* 使用 Prometheus 指标格式.
|
* 使用 Prometheus 指标格式.
|
||||||
*/
|
*/
|
||||||
fun startMetricsServer() {
|
internal fun startMetricsServer(config: MetricsConfig = Const.config.metrics) {
|
||||||
|
if (!config.enable) {
|
||||||
|
log.debug { "运行指标服务器已禁用." }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val builder = HTTPServer.Builder()
|
val builder = HTTPServer.Builder()
|
||||||
.withDaemonThreads(true)
|
.withDaemonThreads(true)
|
||||||
.withPort(Const.config.metrics.port)
|
.withPort(config.port)
|
||||||
.withHostname(Const.config.metrics.bindAddress)
|
.withHostname(config.bindAddress)
|
||||||
|
|
||||||
val httpServer = builder
|
val httpServer = builder
|
||||||
.build()
|
.build()
|
||||||
@ -46,7 +49,7 @@ fun startMetricsServer() {
|
|||||||
log.info { "运行指标服务器已启动. (Port: ${httpServer.port})" }
|
log.info { "运行指标服务器已启动. (Port: ${httpServer.port})" }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class Launcher : AutoCloseable {
|
internal class Launcher(private val config: AppConfig = Const.config) : AutoCloseable {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@ -59,9 +62,9 @@ internal class Launcher : AutoCloseable {
|
|||||||
|
|
||||||
private fun getMavenLocalRepository(): LocalRepository {
|
private fun getMavenLocalRepository(): LocalRepository {
|
||||||
val localPath =
|
val localPath =
|
||||||
if (Const.config.mavenLocalRepository != null && Const.config.mavenLocalRepository.isNotEmpty()) {
|
if (config.mavenLocalRepository != null && config.mavenLocalRepository.isNotEmpty()) {
|
||||||
val repoPath = AppPaths.DATA_ROOT.file.toPath()
|
val repoPath = AppPaths.DATA_ROOT.file.toPath()
|
||||||
.resolve(Const.config.mavenLocalRepository)
|
.resolve(config.mavenLocalRepository)
|
||||||
.toRealPath()
|
.toRealPath()
|
||||||
.toFile()
|
.toFile()
|
||||||
repoPath
|
repoPath
|
||||||
@ -101,15 +104,15 @@ internal class Launcher : AutoCloseable {
|
|||||||
val proxyConfig =
|
val proxyConfig =
|
||||||
if (botConfig.proxy != null && botConfig.proxy.type != DefaultBotOptions.ProxyType.NO_PROXY) {
|
if (botConfig.proxy != null && botConfig.proxy.type != DefaultBotOptions.ProxyType.NO_PROXY) {
|
||||||
botConfig.proxy
|
botConfig.proxy
|
||||||
} else if (Const.config.proxy.type != DefaultBotOptions.ProxyType.NO_PROXY) {
|
} else if (config.proxy.type != DefaultBotOptions.ProxyType.NO_PROXY) {
|
||||||
Const.config.proxy
|
config.proxy
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
if (proxyConfig != null) {
|
if (proxyConfig != null) {
|
||||||
proxyType = proxyConfig.type
|
proxyType = proxyConfig.type
|
||||||
proxyHost = Const.config.proxy.host
|
proxyHost = config.proxy.host
|
||||||
proxyPort = Const.config.proxy.port
|
proxyPort = config.proxy.port
|
||||||
log.debug { "机器人 `${botConfig.account.name}` 已启用代理配置: $proxyConfig" }
|
log.debug { "机器人 `${botConfig.account.name}` 已启用代理配置: $proxyConfig" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,21 +122,21 @@ internal class Launcher : AutoCloseable {
|
|||||||
}
|
}
|
||||||
val account = botConfig.account
|
val account = botConfig.account
|
||||||
|
|
||||||
val remoteRepositories = Const.config.mavenRepositories
|
val remoteRepositories = config.mavenRepositories
|
||||||
.map(MavenRepositoryConfig::toRemoteRepository)
|
.map(MavenRepositoryConfig::toRemoteRepository)
|
||||||
.toMutableList().apply {
|
.toMutableList().apply {
|
||||||
if (this.none {
|
if (this.none {
|
||||||
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL
|
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL
|
||||||
|| it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL.trimEnd('/')
|
|| it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL.trimEnd('/')
|
||||||
}) {
|
}) {
|
||||||
add(MavenRepositoryExtensionFinder.getMavenCentralRepository(proxy = Const.config.proxy.toAetherProxy()))
|
add(MavenRepositoryExtensionFinder.getMavenCentralRepository(proxy = config.proxy.toAetherProxy()))
|
||||||
}
|
}
|
||||||
}.toList()
|
}.toList()
|
||||||
val extensionPackageFinders = setOf(
|
val extensionPackageFinders = setOf(
|
||||||
MavenRepositoryExtensionFinder(
|
MavenRepositoryExtensionFinder(
|
||||||
localRepository = mavenLocalRepository,
|
localRepository = mavenLocalRepository,
|
||||||
remoteRepositories = remoteRepositories,
|
remoteRepositories = remoteRepositories,
|
||||||
proxy = Const.config.proxy.toAetherProxy()
|
proxy = config.proxy.toAetherProxy()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user