支持在安装Systemd服务时通过环境变量读取Discord令牌

如果命令行参数存在,命令行参数的优先级仍然高于环境变量
另外,我不太理解为什么之前要对一个仅有唯一元素的List进行迭代
This commit is contained in:
TheWhiteDog9487
2025-12-12 09:00:23 +08:00
parent 2e34240455
commit 12db682c41
4 changed files with 20 additions and 19 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ plugins {
}
group = "xyz.thewhitedog9487"
version = "0.7.3"
version = "0.7.4"
java {
toolchain {
@@ -35,7 +35,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-webmvc")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
// developmentOnly("org.springframework.boot:spring-boot-docker-compose")
developmentOnly("org.springframework.boot:spring-boot-docker-compose")
runtimeOnly("org.xerial:sqlite-jdbc")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
@@ -23,7 +23,7 @@ class OpenAPIConfiguration {
.contact(new Contact()
.name("TheWhiteDog9487")
.url("https://www.github.com/TheWhiteDog9487/WebAPI"))
.version("0.7.3")
.version("0.7.4")
.license(new License()
.name("WTFPL")
.url("https://spdx.org/licenses/WTFPL")) )
@@ -4,6 +4,7 @@ import discord4j.core.DiscordClientBuilder;
import discord4j.core.GatewayDiscordClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@@ -18,17 +19,17 @@ import java.util.concurrent.locks.ReentrantLock;
@Slf4j
@Component
class GlobalSharedBean {
@Value("${Discord_Bot_Token:}") String DiscordBotToken;
@Bean
GatewayDiscordClient GetDiscordClient(){
String DiscordBotToken = System.getenv("Discord_Bot_Token");
if (DiscordBotToken == null) {
if (DiscordBotToken.isEmpty()) {
log.error("未设置环境变量Discord_Bot_Token,请检查配置。");
System.exit(-1); }
return DiscordClientBuilder.create(DiscordBotToken)
.build()
.login()
.block();}
// TODO: ↑ 看看能不能优化下启动性能
@Bean
List<String> ApiKeyList() {
@@ -10,6 +10,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
@Slf4j
public class SystemdInstallerManager{
@@ -41,7 +42,7 @@ public class SystemdInstallerManager{
static Path ServiceFileName = Path.of("WebAPI.service");
static String SoftLinkFileName = "Current";
static Path SymbolicLinkPath = WorkingDirectory.resolve(SoftLinkFileName);
static String DiscordBotToken = System.getenv("Discord_Bot_Token");
/**
* 检测当前运行环境是否为GraalVM生成的Native Image
*/
@@ -93,14 +94,16 @@ public class SystemdInstallerManager{
在Native Image / Jar文件旁边生成指向自身的,名为Current的软连接
然后在软连接旁生成systemd服务文件,执行目标指向软连接
*/
List<String> DiscordBotToken = Arrays.stream(CommandLineArguments)
.filter(s -> {
for (String o : List.of("--Discord_Bot_Token=")) {
if (s.startsWith(o) == true) { return true; } }
return false; } )
.toList();
if ( DiscordBotToken.isEmpty() == true ) {
log.error("必须通过\"--Discord_Bot_Token=\"参数提供Discord机器人的令牌以使本程序正常工作。");
try {
DiscordBotToken = Arrays.stream(CommandLineArguments)
.filter(s -> s.startsWith("--Discord_Bot_Token=") == true)
.toList()
.getFirst()
.substring("--"
.length()); }
catch (NoSuchElementException _){ }
if ( DiscordBotToken == null ) {
log.error("必须通过\"--Discord_Bot_Token=\"参数或Discord_Bot_Token环境变量提供Discord机器人的令牌以使本程序正常工作。");
throw new IllegalArgumentException("缺少必须的--Discord_Bot_Token参数"); }
log.info("开始安装systemd服务");
log.debug("SystemProperties.os.name: {}", CurrentOS);
@@ -131,10 +134,7 @@ public class SystemdInstallerManager{
CurrentUser,
WorkingDirectory,
ExecCommand,
DiscordBotToken
.getFirst()
.substring("--"
.length()));
"Discord_Bot_Token=" + DiscordBotToken);
log.debug("ServiceFileContent: \n{}", ServiceFileContent);
Files.deleteIfExists( WorkingDirectory.resolve(ServiceFileName) );
Files.writeString(WorkingDirectory.resolve(ServiceFileName), ServiceFileContent);