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