split in env required and dynamic values
This commit is contained in:
parent
f32143b7ac
commit
730c25a9a1
35 changed files with 491 additions and 198 deletions
|
@ -1,57 +1,135 @@
|
|||
import { SettingString, settingsType } from "../type/settingTypes";
|
||||
import ms from "ms";
|
||||
import { EnvSettingString, envSettingsType, SettingString, settingsType } from "../type/settingTypes";
|
||||
import { CodingHelper } from "./codingHelper";
|
||||
import SettingCommandHandler from "../command/settingCommandHandler";
|
||||
import SettingService from "../service/settingService";
|
||||
import { APPLICATION_SECRET } from "../env.defaults";
|
||||
|
||||
export abstract class SettingHelper {
|
||||
export default abstract class SettingHelper {
|
||||
private static settings: { [key in SettingString]?: string } = {};
|
||||
private static envSettings: { [key in EnvSettingString]?: string } = {};
|
||||
|
||||
public static getSetting(key: SettingString): string | number | boolean | ms.StringValue {
|
||||
let settingType = settingsType[key];
|
||||
return this.settings[key] ?? settingType.default ?? "";
|
||||
let setting = this.settings[key] ?? String(settingType.default ?? "");
|
||||
|
||||
if (Array.isArray(settingType.type)) {
|
||||
return setting;
|
||||
}
|
||||
|
||||
if (settingType.type.includes("/crypt")) {
|
||||
setting = CodingHelper.decrypt(APPLICATION_SECRET, String(setting));
|
||||
}
|
||||
|
||||
if (settingType.type.startsWith("string")) {
|
||||
return setting;
|
||||
}
|
||||
if (settingType.type.startsWith("ms")) {
|
||||
return setting as ms.StringValue;
|
||||
}
|
||||
if (settingType.type.startsWith("number")) {
|
||||
return Number(setting);
|
||||
}
|
||||
if (settingType.type.startsWith("boolean")) {
|
||||
return setting == "true";
|
||||
}
|
||||
return setting;
|
||||
}
|
||||
|
||||
public static getEnvSetting(key: EnvSettingString): string {
|
||||
let settingType = envSettingsType[key];
|
||||
return this.envSettings[key] ?? settingType.default ?? "";
|
||||
public static async setSetting(key: SettingString, value: string) {
|
||||
if (value == undefined || value == null) return;
|
||||
let settingType = settingsType[key];
|
||||
|
||||
let result = value;
|
||||
|
||||
this.checkSettings(key, result);
|
||||
|
||||
if (!Array.isArray(settingType.type) && settingType.type.includes("/crypt")) {
|
||||
result = CodingHelper.encrypt(APPLICATION_SECRET, value);
|
||||
}
|
||||
|
||||
await SettingCommandHandler.create({
|
||||
topic: key.split(".")[0],
|
||||
key: key.split(".")[1],
|
||||
value: result,
|
||||
});
|
||||
}
|
||||
|
||||
public static async configure() {}
|
||||
public static async resetSetting(key: SettingString) {
|
||||
let settingType = settingsType[key];
|
||||
this.settings[key] = String(settingType.default ?? "");
|
||||
|
||||
public static async configurEnv() {
|
||||
this.envSettings = {
|
||||
"database.type": process.env.DB_TYPE,
|
||||
"database.host": process.env.DB_HOST,
|
||||
"database.port": process.env.DB_PORT,
|
||||
"database.name": process.env.DB_NAME,
|
||||
"database.username": process.env.DB_USERNAME,
|
||||
"database.password": process.env.DB_PASSWORD,
|
||||
};
|
||||
this.checkEnvSettings();
|
||||
await SettingCommandHandler.delete({
|
||||
topic: key.split(".")[0],
|
||||
key: key.split(".")[1],
|
||||
});
|
||||
}
|
||||
|
||||
private static checkEnvSettings() {
|
||||
if (!["mysql", "sqlite", "postgres"].includes(this.envSettings["database.type"]))
|
||||
throw new Error("set valid value to DB_TYPE (mysql|sqlite|postgres)");
|
||||
if (this.checkIfEmptyOrNotString(this.envSettings["database.name"]))
|
||||
throw new Error("set valid value to DB_NAME (name of database or filepath for sqlite)");
|
||||
public static async configure() {
|
||||
console.log("Configured Settings");
|
||||
let settings = await SettingService.getSettings();
|
||||
|
||||
for (const element of settings) {
|
||||
let ref = `${element.topic}.${element.key}` as SettingString;
|
||||
this.settings[ref] = element.value;
|
||||
this.checkSettings(ref);
|
||||
}
|
||||
}
|
||||
|
||||
private static checkSettings(key: SettingString, value?: string) {
|
||||
let settingType = settingsType[key];
|
||||
|
||||
if (!value) {
|
||||
value = this.getSetting(key).toString();
|
||||
}
|
||||
|
||||
if (
|
||||
this.checkIfEmptyOrNotString(this.envSettings["database.host"]) &&
|
||||
this.envSettings["database.type"] != "sqlite"
|
||||
)
|
||||
throw new Error("set valid value to DB_HOST");
|
||||
!Array.isArray(settingType.type) &&
|
||||
settingType.type.startsWith("string") &&
|
||||
this.checkIfEmptyOrNotString(value)
|
||||
) {
|
||||
throw new Error(`set valid value to ${key}`);
|
||||
}
|
||||
if (
|
||||
this.checkIfEmptyOrNotString(this.envSettings["database.username"]) &&
|
||||
this.envSettings["database.type"] != "sqlite"
|
||||
)
|
||||
throw new Error("set valid value to DB_USERNAME");
|
||||
!Array.isArray(settingType.type) &&
|
||||
settingType.type.startsWith("ms") &&
|
||||
this.checkIfNotMS(value as ms.StringValue)
|
||||
) {
|
||||
throw new Error(`set valid ms value to ${key} -> [0-9]*(y|d|h|m|s)`);
|
||||
}
|
||||
if (!Array.isArray(settingType.type) && settingType.type.startsWith("number") && isNaN(Number(value))) {
|
||||
throw new Error(`set valid numeric value to ${key}`);
|
||||
}
|
||||
if (
|
||||
this.checkIfEmptyOrNotString(this.envSettings["database.password"]) &&
|
||||
this.envSettings["database.type"] != "sqlite"
|
||||
)
|
||||
throw new Error("set valid value to DB_PASSWORD");
|
||||
!Array.isArray(settingType.type) &&
|
||||
settingType.type.startsWith("number") &&
|
||||
settingType.min &&
|
||||
Number(value) < settingType.min
|
||||
) {
|
||||
throw new Error(`${key} has to be at least ${settingType.min}`);
|
||||
}
|
||||
if (
|
||||
!Array.isArray(settingType.type) &&
|
||||
settingType.type.startsWith("boolean") &&
|
||||
value != "true" &&
|
||||
value != "false"
|
||||
) {
|
||||
throw new Error(`"set 'true' or 'false' to ${key}`);
|
||||
}
|
||||
}
|
||||
|
||||
private static checkIfEmptyOrNotString(val: any) {
|
||||
return typeof val != "string" || val == "";
|
||||
}
|
||||
|
||||
private static checkIfNotMS(input: ms.StringValue): boolean {
|
||||
try {
|
||||
const result = ms(input);
|
||||
if (result === undefined) {
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue