This commit is contained in:
2025-08-28 15:22:41 +08:00
parent 6a9d697f6e
commit 77729f0328
3 changed files with 72 additions and 52 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ plugins {
}
group = "xyz.thewhitedog9487"
version = "0.5.0"
version = "0.5.1"
java {
toolchain {
@@ -23,7 +23,7 @@ class OpenAPIConfiguration {
.contact(new Contact()
.name("TheWhiteDog9487")
.url("https://www.github.com/TheWhiteDog9487/WebAPI"))
.version("0.5.0")
.version("0.5.1")
.license(new License()
.name("WTFPL")
.url("https://spdx.org/licenses/WTFPL")) )
@@ -1,6 +1,9 @@
package xyz.thewhitedog9487.WebAPI.Startup;
import discord4j.core.GatewayDiscordClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@@ -8,6 +11,7 @@ import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
@Slf4j
@@ -24,11 +28,13 @@ class SystemdInstallerManager implements CommandLineRunner {
ExecStart=%s
Restart=always
Type=simple
Environment="%s"
[Install]
WantedBy=multi-user.target
""";
@Autowired ObjectProvider<GatewayDiscordClient> DiscordClientProvider;
String CurrentOS = System.getProperty("os.name");
String CurrentUser = System.getProperty("user.name");
Path CurrentExecutableFilePath = GetExecutblePath();
@@ -90,6 +96,15 @@ class SystemdInstallerManager implements CommandLineRunner {
在Native Image / Jar文件旁边生成指向自身的,名为Current的软连接
然后在软连接旁生成systemd服务文件,执行目标指向软连接
*/
List<String> DiscordBotToken = Arrays.stream(args)
.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机器人的令牌以使本程序正常工作。");
throw new IllegalArgumentException("缺少必须的--Discord_Bot_Token参数"); }
log.info("开始安装systemd服务");
log.debug("SystemProperties.os.name: {}", CurrentOS);
if ( CurrentOS.startsWith("Linux") ) {
@@ -116,7 +131,11 @@ class SystemdInstallerManager implements CommandLineRunner {
TemplateContent,
CurrentUser,
WorkingDirectory,
ExecCommand);
ExecCommand,
DiscordBotToken
.getFirst()
.substring("--"
.length()));
log.debug("ServiceFileContent: \n{}", ServiceFileContent);
Files.deleteIfExists( WorkingDirectory.resolve(ServiceFileName) );
Files.writeString(WorkingDirectory.resolve(ServiceFileName), ServiceFileContent);
@@ -133,8 +152,7 @@ class SystemdInstallerManager implements CommandLineRunner {
else if ( CurrentOS.startsWith("Windows") ) {
log.error("只有Linux才能使用systemd,但是JVM报告的当前系统是{}", CurrentOS);
throw new IllegalStateException("Windows系统不支持systemd服务管理,暂不支持在Windows上自动管理服务。"); }
System.exit(0);
}
System.exit(0); }
else if ( List.of("uninstall", "--uninstall").contains(Argument) == true ) {
log.info("开始卸载systemd服务");
log.debug("SystemProperties.os.name: {}", CurrentOS);
@@ -157,4 +175,6 @@ class SystemdInstallerManager implements CommandLineRunner {
log.info("成功删除指向程序自身的软链接: {}", SymbolicLinkPath);
new ProcessBuilder("systemctl", "daemon-reload").start().waitFor();
log.info("已完成systemd守护进程重载"); }
System.exit(0); } } } }
System.exit(0); } }
// ↓ 并不是安装或卸载服务,于是继续正常的启动程序,让Discord客户端连接服务器,校验API密钥是否正确并确保密钥非空
DiscordClientProvider.getIfAvailable(); } }