【8.3.0】【config】增加一个系统配置类型,标识本地文件存储路径

master
stylefeng 2024-08-29 23:16:02 +08:00
parent 6089b7c018
commit 5bee25d273
7 changed files with 294 additions and 2 deletions

View File

@ -52,4 +52,26 @@ public interface ConfigConstants {
*/ */
String CONFIG_GROUP_DICT_TYPE_CODE = "config_group"; String CONFIG_GROUP_DICT_TYPE_CODE = "config_group";
/**
*
* <p>
* 10-jar
* 11-linuxwindows
* 20-MinIO
* 30-
* 40-
* 50-
*/
String SYS_FILE_SAVE_TYPE_CONFIG_CODE = "SYS_FILE_SAVE_TYPE";
/**
* LINUX
*/
String SYS_LOCAL_FILE_SAVE_PATH_LINUX = "SYS_LOCAL_FILE_SAVE_PATH_LINUX";
/**
* windows
*/
String SYS_LOCAL_FILE_SAVE_PATH_WINDOWS = "SYS_LOCAL_FILE_SAVE_PATH_WINDOWS";
} }

View File

@ -0,0 +1,132 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.config.api.enums;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.base.ReadableEnum;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
/**
*
* <p>
* 10-jar
* 11-linuxwindows
* 20-MinIO
* 30-
* 40-
* 50-
*
* @author fengshuonan
* @since 2024/8/29 21:44
*/
@Getter
public enum FileStorageTypeEnum implements ReadableEnum<FileStorageTypeEnum> {
/**
* jar
*/
LOCAL_DEFAULT(10, "本地存储到默认路径jar所在目录"),
/**
* linuxwindows
*/
LOCAL(11, "本地存储到指定路径下需要配置linux和windows的路径"),
/**
* MinIO
*/
MINIO(20, "存储到MinIO"),
/**
*
*/
ALIYUN(30, "存储到阿里云"),
/**
*
*/
TEN_CLOUD(40, "存储到腾讯云"),
/**
*
*/
QING_CLOUD(50, "青云");
@EnumValue
@JsonValue
private final Integer code;
private final String message;
FileStorageTypeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
/**
* code
*
* @author fengshuonan
* @since 2020/10/29 18:59
*/
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static FileStorageTypeEnum codeToEnum(Integer code) {
if (null != code) {
for (FileStorageTypeEnum item : FileStorageTypeEnum.values()) {
if (item.getCode().equals(code)) {
return item;
}
}
}
return null;
}
@Override
public Object getKey() {
return this.code;
}
@Override
public Object getName() {
return this.message;
}
@Override
public FileStorageTypeEnum parseToEnum(String originValue) {
if (ObjectUtil.isEmpty(originValue)) {
return null;
}
for (FileStorageTypeEnum value : FileStorageTypeEnum.values()) {
if (value.code.equals(Convert.toInt(originValue))) {
return value;
}
}
return null;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.config.modular.listener;
import cn.stylefeng.roses.kernel.config.modular.service.SysConfigService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
*
*
* @author fengshuonan
* @since 2024/8/29 21:33
*/
@Slf4j
@Component
public class LocalStorageListener implements CommandLineRunner {
@Resource
private SysConfigService sysConfigService;
@Override
public void run(String... args) {
this.sysConfigService.initLocalDefaultStoragePath();
}
}

View File

@ -131,4 +131,21 @@ public interface SysConfigService extends IService<SysConfig>, InitConfigApi, Co
*/ */
void updateSysConfigTypeCode(String originTypeCode, String destTypeCode); void updateSysConfigTypeCode(String originTypeCode, String destTypeCode);
/**
*
*
* @author fengshuonan
* @since 2024/8/29 21:35
*/
String getConfigValueByCode(String configCode);
/**
*
*
* @return
* @author fengshuonan
* @since 2024/8/29 22:16
*/
String initLocalDefaultStoragePath();
} }

View File

@ -29,9 +29,11 @@ import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.system.SystemUtil;
import cn.stylefeng.roses.kernel.config.api.ConfigInitCallbackApi; import cn.stylefeng.roses.kernel.config.api.ConfigInitCallbackApi;
import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi; import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext; import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import cn.stylefeng.roses.kernel.config.api.enums.FileStorageTypeEnum;
import cn.stylefeng.roses.kernel.config.api.exception.ConfigException; import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum; import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitItem; import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitItem;
@ -50,6 +52,7 @@ import cn.stylefeng.roses.kernel.rule.callback.ConfigUpdateCallback;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants; import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum; import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum; import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.util.JarPathUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -61,6 +64,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static cn.stylefeng.roses.kernel.config.api.constants.ConfigConstants.*;
/** /**
* service * service
* *
@ -277,6 +282,46 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
this.update(sysConfigLambdaUpdateWrapper); this.update(sysConfigLambdaUpdateWrapper);
} }
@Override
public String getConfigValueByCode(String configCode) {
LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysConfig::getConfigCode, configCode);
wrapper.select(SysConfig::getConfigId, SysConfig::getConfigValue);
SysConfig sysConfig = this.getOne(wrapper, false);
if (sysConfig == null) {
return "";
} else {
return sysConfig.getConfigValue();
}
}
@Override
public String initLocalDefaultStoragePath() {
// 1. 获取文件存储的配置类型,是否为默认,还是指定位置存储
String configValueByCode = this.getConfigValueByCode(SYS_FILE_SAVE_TYPE_CONFIG_CODE);
Integer configValue = Convert.toInt(configValueByCode);
// 2. 如果不是存储到默认路径,直接跳过本类执行
if (ObjectUtil.notEqual(configValue, FileStorageTypeEnum.LOCAL_DEFAULT.getCode())) {
return null;
}
// 3. 获取当前jar启动的位置的目录
String currentJarPath = JarPathUtil.getJarPath();
if (StrUtil.isBlank(currentJarPath)) {
return null;
}
// 4. 获取当前系统的类型是windows还是linux
if (SystemUtil.getOsInfo().isWindows()) {
this.updateConfigByCode(SYS_LOCAL_FILE_SAVE_PATH_WINDOWS, currentJarPath);
} else {
this.updateConfigByCode(SYS_LOCAL_FILE_SAVE_PATH_LINUX, currentJarPath);
}
return currentJarPath;
}
/** /**
* *
* *

View File

@ -2,8 +2,14 @@ package cn.stylefeng.roses.kernel.config.modular.strategy;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi; import cn.stylefeng.roses.kernel.config.api.ConfigInitStrategyApi;
import cn.stylefeng.roses.kernel.config.api.constants.ConfigConstants;
import cn.stylefeng.roses.kernel.config.api.enums.FileStorageTypeEnum;
import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitItem; import cn.stylefeng.roses.kernel.config.api.pojo.ConfigInitItem;
import cn.stylefeng.roses.kernel.config.modular.service.SysConfigService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,6 +25,9 @@ import java.util.List;
@Component @Component
public class DefaultStrategyImpl implements ConfigInitStrategyApi { public class DefaultStrategyImpl implements ConfigInitStrategyApi {
@Resource
private SysConfigService sysConfigService;
@Override @Override
public String getTitle() { public String getTitle() {
return "系统参数"; return "系统参数";
@ -37,14 +46,29 @@ public class DefaultStrategyImpl implements ConfigInitStrategyApi {
configInitItems.add(new ConfigInitItem("auth认证用的jwt秘钥", "SYS_AUTH_JWT_SECRET", RandomUtil.randomString(30), "用于校验登录token已随机生成一个30位密钥请放心使用")); configInitItems.add(new ConfigInitItem("auth认证用的jwt秘钥", "SYS_AUTH_JWT_SECRET", RandomUtil.randomString(30), "用于校验登录token已随机生成一个30位密钥请放心使用"));
configInitItems.add(new ConfigInitItem("Druid控制台账号", "SYS_DRUID_ACCOUNT", "admin", "Druid控制台账号")); configInitItems.add(new ConfigInitItem("Druid控制台账号", "SYS_DRUID_ACCOUNT", "admin", "Druid控制台账号"));
configInitItems.add(new ConfigInitItem("Druid控制台账号密码", "SYS_DRUID_PASSWORD", RandomUtil.randomString(20), "Druid控制台账号密码已随机生成一个20位密钥请放心使用")); configInitItems.add(new ConfigInitItem("Druid控制台账号密码", "SYS_DRUID_PASSWORD", RandomUtil.randomString(20), "Druid控制台账号密码已随机生成一个20位密钥请放心使用"));
configInitItems.add(new ConfigInitItem("Linux本地文件保存路径", "SYS_LOCAL_FILE_SAVE_PATH_LINUX", "/opt/gunsFilePath", "本地文件存储的路径,上线请注意别使用/tmp目录如果没有用本地文件存储可忽略此配置"));
configInitItems.add(new ConfigInitItem("Windows本地文件保存路径", "SYS_LOCAL_FILE_SAVE_PATH_WINDOWS", "D:\\tempFilePath", "本地文件存储的路径,如果没有用本地文件存储,可忽略此配置"));
configInitItems.add(new ConfigInitItem("session过期时间", "SYS_SESSION_EXPIRED_SECONDS", "3600", "单位session的过期时间这个时间段内不操作会自动踢下线")); configInitItems.add(new ConfigInitItem("session过期时间", "SYS_SESSION_EXPIRED_SECONDS", "3600", "单位session的过期时间这个时间段内不操作会自动踢下线"));
configInitItems.add(new ConfigInitItem("账号单端登录限制", "SYS_SINGLE_ACCOUNT_LOGIN_FLAG", "false", "如果开启,则同一个账号只能一个地方登录")); configInitItems.add(new ConfigInitItem("账号单端登录限制", "SYS_SINGLE_ACCOUNT_LOGIN_FLAG", "false", "如果开启,则同一个账号只能一个地方登录"));
configInitItems.add(new ConfigInitItem("系统默认密码", "SYS_DEFAULT_PASSWORD", "123456", "用在重置密码的默认密码")); configInitItems.add(new ConfigInitItem("系统默认密码", "SYS_DEFAULT_PASSWORD", "123456", "用在重置密码的默认密码"));
configInitItems.add(new ConfigInitItem("系统发布版本", "SYS_RELEASE_VERSION", DateUtil.format(new Date(), "yyyyMMdd"), "系统发布的版本号")); configInitItems.add(new ConfigInitItem("系统发布版本", "SYS_RELEASE_VERSION", DateUtil.format(new Date(), "yyyyMMdd"), "系统发布的版本号"));
configInitItems.add(new ConfigInitItem("数据库加密AES秘钥", "SYS_ENCRYPT_SECRET_KEY", RandomUtil.randomString(32), "对称加密秘钥,用在数据库数据加密")); configInitItems.add(new ConfigInitItem("数据库加密AES秘钥", "SYS_ENCRYPT_SECRET_KEY", RandomUtil.randomString(32), "对称加密秘钥,用在数据库数据加密"));
configInitItems.add(new ConfigInitItem("SSO服务端加密Token信息秘钥", "SYS_AUTH_SSO_DECRYPT_DATA_SECRET", RandomUtil.randomString(32), "SSO服务端加密Token信息秘钥用在单点登录认证时候加密生成Token")); configInitItems.add(new ConfigInitItem("SSO服务端加密Token信息秘钥", "SYS_AUTH_SSO_DECRYPT_DATA_SECRET", RandomUtil.randomString(32), "SSO服务端加密Token信息秘钥用在单点登录认证时候加密生成Token"));
// 【2024年8月29日新增】设置文件存储的类型10-本地存储jar启动路径
configInitItems.add(new ConfigInitItem("文件存储类型", ConfigConstants.SYS_FILE_SAVE_TYPE_CONFIG_CODE, String.valueOf(FileStorageTypeEnum.LOCAL_DEFAULT.getCode()), "文件存储位置类型10-本地存储jar包所在位置11-本地存储(指定位置)"));
// 【2024年8月29日新增】获取初始化路径地址
String localDefaultStoragePath = sysConfigService.initLocalDefaultStoragePath();
if(StrUtil.isBlank(localDefaultStoragePath)){
configInitItems.add(new ConfigInitItem("Windows本地文件保存路径", "SYS_LOCAL_FILE_SAVE_PATH_WINDOWS", "D:\\tempFilePath", "本地文件存储的路径,如果没有用本地文件存储,可忽略此配置"));
configInitItems.add(new ConfigInitItem("Linux本地文件保存路径", "SYS_LOCAL_FILE_SAVE_PATH_LINUX", "/opt/gunsFilePath", "本地文件存储的路径,上线请注意别使用/tmp目录如果没有用本地文件存储可忽略此配置"));
}else{
if(SystemUtil.getOsInfo().isWindows()){
configInitItems.add(new ConfigInitItem("Windows本地文件保存路径", "SYS_LOCAL_FILE_SAVE_PATH_WINDOWS", localDefaultStoragePath, "本地文件存储的路径,如果没有用本地文件存储,可忽略此配置"));
}else{
configInitItems.add(new ConfigInitItem("Linux本地文件保存路径", "SYS_LOCAL_FILE_SAVE_PATH_LINUX", localDefaultStoragePath, "本地文件存储的路径,上线请注意别使用/tmp目录如果没有用本地文件存储可忽略此配置"));
}
}
return configInitItems; return configInitItems;
} }

View File

@ -0,0 +1 @@
INSERT INTO `sys_config`(`config_id`, `config_name`, `config_code`, `config_value`, `sys_flag`, `remark`, `status_flag`, `group_code`, `del_flag`, `create_time`, `create_user`, `update_time`, `update_user`) VALUES (1829152372339773442, '文件存储类型', 'SYS_FILE_SAVE_TYPE', '10', 'Y', '10-本地存储到默认路径jar所在目录\n11-本地存储到指定路径下需要配置linux和windows的路径\n20-存储到MinIO\n30-存储到阿里云\n40-存储到腾讯云\n50-存储到青云', 1, 'file_config', 'N', '2024-08-29 21:41:24', 1821938198325960706, NULL, NULL);