mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
feat: cache config
This commit is contained in:
parent
5cf9a6e942
commit
6e97044437
@ -3,13 +3,27 @@ import {Config} from "./types";
|
||||
const fs = require("fs");
|
||||
|
||||
export class ConfigUtil {
|
||||
configPath: string;
|
||||
private readonly configPath: string;
|
||||
private config: Config | null = null;
|
||||
|
||||
constructor(configPath: string) {
|
||||
this.configPath = configPath;
|
||||
}
|
||||
|
||||
getConfig(): Config {
|
||||
if (this.config) {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
this.reloadConfig();
|
||||
}
|
||||
|
||||
setConfig(config: Config) {
|
||||
this.config = config;
|
||||
fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), "utf-8");
|
||||
}
|
||||
|
||||
reloadConfig() {
|
||||
let defaultConfig: Config = {
|
||||
httpPort: 3000,
|
||||
httpHosts: [],
|
||||
@ -20,36 +34,35 @@ export class ConfigUtil {
|
||||
debug: false,
|
||||
log: false,
|
||||
reportSelfMessage: false
|
||||
}
|
||||
};
|
||||
|
||||
if (!fs.existsSync(this.configPath)) {
|
||||
return defaultConfig
|
||||
} else {
|
||||
const data = fs.readFileSync(this.configPath, "utf-8");
|
||||
let jsonData: Config = defaultConfig;
|
||||
try {
|
||||
jsonData = JSON.parse(data)
|
||||
jsonData = JSON.parse(data);
|
||||
}
|
||||
catch (e) {}
|
||||
|
||||
if (!jsonData.httpHosts) {
|
||||
jsonData.httpHosts = []
|
||||
jsonData.httpHosts = [];
|
||||
}
|
||||
if (!jsonData.wsHosts) {
|
||||
jsonData.wsHosts = []
|
||||
jsonData.wsHosts = [];
|
||||
}
|
||||
if (!jsonData.wsPort) {
|
||||
jsonData.wsPort = 3001
|
||||
jsonData.wsPort = 3001;
|
||||
}
|
||||
if (!jsonData.httpPort) {
|
||||
jsonData.httpPort = 3000
|
||||
jsonData.httpPort = 3000;
|
||||
}
|
||||
if (!jsonData.token) {
|
||||
jsonData.token = ""
|
||||
jsonData.token = "";
|
||||
}
|
||||
|
||||
return jsonData;
|
||||
}
|
||||
}
|
||||
|
||||
setConfig(config: Config) {
|
||||
fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), "utf-8")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user