fix: 鉴权头和sql配置初始化
This commit is contained in:
parent
7ba5d9f69a
commit
993ecfc84c
|
|
@ -5,6 +5,7 @@
|
|||
<option name="customHeaders">
|
||||
<set>
|
||||
<option value="X-API-Key" />
|
||||
<option value="X-API-TOKEN" />
|
||||
</set>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
|
|
|
|||
10
.idea/sqldialects.xml
Normal file
10
.idea/sqldialects.xml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/config/sql/invitation/query_code_ids.sql" dialect="PostgreSQL" />
|
||||
<file url="file://$PROJECT_DIR$/config/sql/invitation/query_qualification.sql" dialect="PostgreSQL" />
|
||||
<file url="file://$PROJECT_DIR$/config/sql/invitation/query_token_info.sql" dialect="PostgreSQL" />
|
||||
<file url="file://$PROJECT_DIR$/config/sql/invitation/upsert_ascription.sql" dialect="PostgreSQL" />
|
||||
<file url="file://$PROJECT_DIR$/config/sql/whitelist/query_whitelist_record.sql" dialect="PostgreSQL" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -3,5 +3,5 @@ org.gradle.downloadSources=false
|
|||
org.gradle.parallel=true
|
||||
org.gradle.degree_of_parallelism=16
|
||||
project_group=top.r3944realms.ltdmanager
|
||||
project_version=1.22.2
|
||||
project_version=1.22.3
|
||||
dg_lab_version=4.4.14.19
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ object YamlConfigLoader {
|
|||
init {
|
||||
ConfigInitializer.initConfig("module.yaml", "config", false)
|
||||
ConfigInitializer.initConfig("application.yaml", "config")
|
||||
ConfigInitializer.initSqlTemplates()
|
||||
runCatching {
|
||||
ensureConfigEncrypted(_app_config)
|
||||
}.onFailure { e ->
|
||||
|
|
|
|||
|
|
@ -30,10 +30,17 @@ object ModuleFactory {
|
|||
UNKNOWN_MODULE -> throw ConfigError(ConfigError.Type.INVALID_PARAMETER, "unknown module")
|
||||
}
|
||||
}
|
||||
private fun resolveDependency(dep: ModuleConfig.Module.Dependency?, name: String): BaseModule? {
|
||||
if (dep != null) {
|
||||
return GlobalManager.moduleManager.getModule(dep.getDepName())
|
||||
} else throw ConfigError (ConfigError.Type.MISSING_PARAMETER, "dependency", name)
|
||||
private fun resolveDependency(dep: ModuleConfig.Module.Dependency?, name: String): BaseModule {
|
||||
if (dep == null) {
|
||||
throw ConfigError(ConfigError.Type.MISSING_PARAMETER, "dependency", name)
|
||||
}
|
||||
val depName = dep.getDepName()
|
||||
return GlobalManager.moduleManager.getModule(depName)
|
||||
?: throw ConfigError(
|
||||
ConfigError.Type.OTHER,
|
||||
"resolveDependency",
|
||||
"依赖模块 '$depName' 未注册 — 请检查 name/type 是否匹配且目标模块定义在 WHITELIST_AUDIT_MODULE 之前"
|
||||
)
|
||||
}
|
||||
private fun createGroupMessagePolling(config: ModuleConfig.Module): GroupMessagePollingModule {
|
||||
val targetGroupId = config.long("target-group-id")
|
||||
|
|
|
|||
|
|
@ -2,17 +2,21 @@ package top.r3944realms.ltdmanager.utils
|
|||
|
||||
import top.r3944realms.ltdmanager.core.config.YamlConfigLoader
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.nio.file.StandardCopyOption
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
object ConfigInitializer {
|
||||
|
||||
/**
|
||||
* 初始化配置文件
|
||||
* @param fileName YAML 文件名,如 application.yml
|
||||
* @param configDir 配置目录,如 config
|
||||
*/
|
||||
private val SQL_TEMPLATES = listOf(
|
||||
"sql/invitation/query_qualification.sql",
|
||||
"sql/invitation/query_token_info.sql",
|
||||
"sql/invitation/query_code_ids.sql",
|
||||
"sql/invitation/upsert_ascription.sql",
|
||||
"sql/whitelist/query_whitelist_record.sql",
|
||||
)
|
||||
|
||||
fun initConfig(fileName: String = "application.yml", configDir: String = "config", shouldExit: Boolean = true) {
|
||||
val dirPath = Paths.get(configDir)
|
||||
if (!Files.exists(dirPath)) {
|
||||
|
|
@ -23,7 +27,6 @@ object ConfigInitializer {
|
|||
val filePath = dirPath.resolve(fileName)
|
||||
|
||||
if (!Files.exists(filePath)) {
|
||||
// 从 resources 复制默认配置
|
||||
val resourceStream = YamlConfigLoader::class.java.classLoader.getResourceAsStream(fileName)
|
||||
if (resourceStream != null) {
|
||||
Files.copy(resourceStream, filePath, StandardCopyOption.REPLACE_EXISTING)
|
||||
|
|
@ -33,9 +36,31 @@ object ConfigInitializer {
|
|||
exitProcess(-1);
|
||||
}
|
||||
} else throw Error("Jar内部资源文件缺失")
|
||||
|
||||
} else {
|
||||
LoggerUtil.logger.info("配置文件已存在: $filePath")
|
||||
}
|
||||
}
|
||||
|
||||
/** 首次启动时将 SQL 模板从 classpath 复制到 config/sql/ */
|
||||
fun initSqlTemplates() {
|
||||
var copied = 0
|
||||
for (resourcePath in SQL_TEMPLATES) {
|
||||
val target = Paths.get("config", resourcePath)
|
||||
if (Files.exists(target)) continue
|
||||
|
||||
val resourceStream = YamlConfigLoader::class.java.classLoader
|
||||
.getResourceAsStream(resourcePath)
|
||||
if (resourceStream != null) {
|
||||
Files.createDirectories(target.parent)
|
||||
Files.copy(resourceStream, target)
|
||||
resourceStream.close()
|
||||
copied++
|
||||
} else {
|
||||
LoggerUtil.logger.warn("SQL 模板资源缺失: $resourcePath")
|
||||
}
|
||||
}
|
||||
if (copied > 0) {
|
||||
LoggerUtil.logger.info("已从 JAR 复制 $copied 个 SQL 模板到 config/sql/")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
package top.r3944realms.ltdmanager.whitelist.request
|
||||
|
||||
import io.ktor.http.HeadersBuilder
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import top.r3944realms.ltdmanager.core.client.request.IRequest
|
||||
import top.r3944realms.ltdmanager.core.config.YamlConfigLoader
|
||||
import top.r3944realms.ltdmanager.whitelist.response.FailedWhitelistSystemResponse
|
||||
import top.r3944realms.ltdmanager.whitelist.response.WhitelistSystemResponse
|
||||
|
||||
|
|
@ -10,4 +12,14 @@ import top.r3944realms.ltdmanager.whitelist.response.WhitelistSystemResponse
|
|||
abstract class WhitelistSystemRequest(
|
||||
@Transient
|
||||
override val createTime: Long = System.currentTimeMillis()
|
||||
) : IRequest<WhitelistSystemResponse, FailedWhitelistSystemResponse>
|
||||
) : IRequest<WhitelistSystemResponse, FailedWhitelistSystemResponse> {
|
||||
|
||||
/** 自动注入 X-API-TOKEN 鉴权头 */
|
||||
override fun headers(): HeadersBuilder.() -> Unit = {
|
||||
super.headers().invoke(this)
|
||||
val token = YamlConfigLoader.loadWhitelistSystemConfig().decryptedToken
|
||||
if (!token.isNullOrEmpty()) {
|
||||
append("X-API-TOKEN", token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user