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