mirror of
https://github.com/Geniusay/ChopperBot.git
synced 2026-05-30 02:20:12 +08:00
配置文件架构的小完善
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package org.example.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
* @date 2023/04/20 19:29
|
||||
**/
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ModuleConfigSrc {
|
||||
private String src;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.example.bean;
|
||||
|
||||
import org.example.common.ConfigFile;
|
||||
import org.example.constpool.ConstPool;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
* @date 2023/04/20 19:29
|
||||
**/
|
||||
|
||||
|
||||
public class ModuleSrcConfigFile extends ConfigFile<Map<String, ModuleSrcConfigFile.SRC>> {
|
||||
|
||||
private static final Map<String, SRC> config;
|
||||
|
||||
public static class SRC{
|
||||
private String src;
|
||||
public SRC(String src) {
|
||||
this.src = src;
|
||||
}
|
||||
|
||||
public String getSrc() {
|
||||
return src;
|
||||
}
|
||||
}
|
||||
|
||||
static{
|
||||
config = Map.of(
|
||||
ConstPool.ACCOUNT, new SRC("./config/"+ConstPool.ACCOUNT),
|
||||
ConstPool.SECTION, new SRC("./config/"+ConstPool.SECTION),
|
||||
ConstPool.BARRAGE, new SRC("./config/"+ConstPool.BARRAGE),
|
||||
ConstPool.CREEPER, new SRC("./config/"+ConstPool.CREEPER),
|
||||
ConstPool.SECTION_WORK, new SRC("./config/"+ConstPool.SECTION_WORK),
|
||||
ConstPool.HOT, new SRC("./config/"+ConstPool.HOT),
|
||||
ConstPool.PUBLISH, new SRC("./config/"+ConstPool.PUBLISH)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public ModuleSrcConfigFile() {
|
||||
super("./config"
|
||||
, "moduleConfig.json"
|
||||
, config);
|
||||
}
|
||||
|
||||
public Map<String,Object> packageConfig() {
|
||||
return super.packageConfig();
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package org.example.init;
|
||||
|
||||
import org.example.bean.ModuleConfigSrc;
|
||||
import org.example.util.FileUtil;
|
||||
import org.example.util.JsonFileUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
* @date 2023/04/20 18:34
|
||||
**/
|
||||
|
||||
public class ModuleConfigSrcInit implements InitMachine {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ModuleConfigSrcInit.class);
|
||||
private static final Map<String, ModuleConfigSrc> config;
|
||||
|
||||
static{
|
||||
config = Map.of(
|
||||
"account", new ModuleConfigSrc("./config/account"),
|
||||
"section", new ModuleConfigSrc("./config/section"),
|
||||
"barrage", new ModuleConfigSrc("./config/barrage"),
|
||||
"creeper", new ModuleConfigSrc("./config/creeper"),
|
||||
"videowork", new ModuleConfigSrc("./config/videowork"),
|
||||
"hot", new ModuleConfigSrc("./config/hot"),
|
||||
"publish", new ModuleConfigSrc("./config/publish")
|
||||
);
|
||||
}
|
||||
|
||||
private static final String CONFIG_SRC = "./config";
|
||||
private static final String CONFIG_SRC_FILE = "moduleConfig.json";
|
||||
|
||||
// 初始化每个模块的配置文件夹
|
||||
@Override
|
||||
public boolean init() {
|
||||
Path dir = Paths.get(CONFIG_SRC);
|
||||
if (!createConfigDirectory(dir)) {
|
||||
return false;
|
||||
}
|
||||
if (!createConfigFile(dir)) {
|
||||
return false;
|
||||
}
|
||||
if (!createModuleDirectory()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean createConfigDirectory(Path dir) {
|
||||
try {
|
||||
if (!Files.exists(dir)) {
|
||||
Files.createDirectory(dir);
|
||||
logger.info("创建 config 文件夹成功 √ ");
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建配置文件夹失败");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean createConfigFile(Path dir) {
|
||||
Path path = Paths.get(dir.toString(), CONFIG_SRC_FILE);
|
||||
try {
|
||||
if (!Files.exists(path)) {
|
||||
JsonFileUtil.writeJsonFile(path.toString(),config);
|
||||
logger.info("创建 {} 配置文件成功 √",CONFIG_SRC_FILE);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建配置文件失败");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean createModuleDirectory() {
|
||||
for (Map.Entry<String, ModuleConfigSrc> stringModuleConfigSrcEntry : config.entrySet()) {
|
||||
ModuleConfigSrc moduleConfigSrc = stringModuleConfigSrcEntry.getValue();
|
||||
try {
|
||||
if (!FileUtil.isFileExist(moduleConfigSrc.getSrc())) {
|
||||
Files.createDirectory(Path.of(moduleConfigSrc.getSrc()));
|
||||
logger.info("创建 {} 模块文件夹成功 √ ",moduleConfigSrc.getSrc());
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建 {} 模块文件夹失败 ×",moduleConfigSrc.getSrc());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.example.init;
|
||||
|
||||
import org.example.bean.ModuleSrcConfigFile;
|
||||
import org.example.util.FileUtil;
|
||||
import org.example.util.JsonFileUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
* @date 2023/04/20 18:34
|
||||
**/
|
||||
|
||||
public class ModuleSrcConfigFileInit implements InitMachine {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ModuleSrcConfigFileInit.class);
|
||||
|
||||
ModuleSrcConfigFile moduleSrcConfigFile;
|
||||
public ModuleSrcConfigFileInit() {
|
||||
moduleSrcConfigFile = new ModuleSrcConfigFile();
|
||||
}
|
||||
@Override
|
||||
public boolean init() {
|
||||
Path dir = Paths.get(moduleSrcConfigFile.getFilePath());
|
||||
if (!createConfigDirectory(dir)) {
|
||||
return false;
|
||||
}
|
||||
if (!createConfigFile(dir)) {
|
||||
return false;
|
||||
}
|
||||
if (!createModuleDirectory()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean createConfigDirectory(Path dir) {
|
||||
try {
|
||||
if (!Files.exists(dir)) {
|
||||
Files.createDirectory(dir);
|
||||
logger.info("创建 config 文件夹成功 √ ");
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建配置文件夹失败");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean createConfigFile(Path dir) {
|
||||
Path path = Paths.get(dir.toString(), moduleSrcConfigFile.getFileName());
|
||||
try {
|
||||
if (!Files.exists(path)) {
|
||||
JsonFileUtil.writeJsonFile(path.toString(),moduleSrcConfigFile.packageConfig());
|
||||
logger.info("创建 {} 配置文件成功 √",moduleSrcConfigFile.getFileName());
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建配置文件失败");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean createModuleDirectory() {
|
||||
|
||||
Map<String, ModuleSrcConfigFile.SRC> moduleSrcConfigFileMap
|
||||
= (Map<String, ModuleSrcConfigFile.SRC>) moduleSrcConfigFile.packageConfig().get("data");
|
||||
|
||||
for (Map.Entry<String, ModuleSrcConfigFile.SRC> stringModuleConfigSrcEntry : moduleSrcConfigFileMap.entrySet()) {
|
||||
|
||||
ModuleSrcConfigFile.SRC src = stringModuleConfigSrcEntry.getValue();
|
||||
try {
|
||||
if (!FileUtil.isFileExist(src.getSrc())) {
|
||||
Files.createDirectory(Path.of(src.getSrc()));
|
||||
logger.info("创建 {} 模块文件夹成功 √ ",src.getSrc());
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("创建 {} 模块文件夹失败 ×",src.getSrc());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1 +1,18 @@
|
||||
[{"age":18,"hobby":["Coding","Reading","Playing"],"info":{"QQ":"123456789","WeChat":"987654321"},"major":"CS","name":"Genius","school":"HUST"}]
|
||||
{
|
||||
"hello":"11234",
|
||||
"Student":{
|
||||
"age":18,
|
||||
"hobby":[
|
||||
"Coding",
|
||||
"Reading",
|
||||
"Playing"
|
||||
],
|
||||
"info":{
|
||||
"QQ":"123456789",
|
||||
"WeChat":"987654321"
|
||||
},
|
||||
"major":"CS",
|
||||
"name":"Genius",
|
||||
"school":"HUST"
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,26 @@ import java.util.Map;
|
||||
|
||||
public class JsonFileUtilTest {
|
||||
|
||||
@Test
|
||||
public void writeMapObj() throws IOException {
|
||||
Student student = new Student(
|
||||
"Genius",
|
||||
18,
|
||||
"HUST",
|
||||
"CS",
|
||||
List.of("Coding", "Reading", "Playing"),
|
||||
Map.of("QQ", "123456789", "WeChat", "987654321")
|
||||
);
|
||||
JsonFileUtil.writeJsonFile("E:\\Project\\ChopperBot\\FileModule\\src\\main\\resources\\student2.json",
|
||||
Map.of("Student", student,"hello","11234"));
|
||||
Map<String, Object> stringObjectMap = JsonFileUtil.readJsonFile("E:\\Project\\ChopperBot\\FileModule\\src\\main\\resources\\student2.json");
|
||||
stringObjectMap.forEach(
|
||||
(k,v)->{
|
||||
System.out.println(k);
|
||||
System.out.println(v.getClass());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@@ -64,4 +84,5 @@ public class JsonFileUtilTest {
|
||||
JsonFileUtil.writeBigJsonFile(dir.toString(),maps);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user