2023-06-09 03:58:38 +00:00
|
|
|
import { readFileSync, writeFileSync } from "fs";
|
|
|
|
import { exit } from "process";
|
|
|
|
|
2023-06-29 21:35:19 +00:00
|
|
|
class LocalDB {
|
2023-06-09 03:58:38 +00:00
|
|
|
#filename = "config/localdb.json";
|
|
|
|
#data = null;
|
2023-06-29 21:35:19 +00:00
|
|
|
constructor () {
|
2023-06-09 03:58:38 +00:00
|
|
|
try {
|
|
|
|
this.load(this.#filename);
|
2023-06-29 21:35:19 +00:00
|
|
|
} catch {
|
2023-06-09 03:58:38 +00:00
|
|
|
console.log("Error: localdb.json was not found. Please follow the directions in the README to initialize localdb.json.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
|
|
|
load (path) {
|
2023-06-09 03:58:38 +00:00
|
|
|
this.#data = JSON.parse(readFileSync(path));
|
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
|
|
|
save (path) {
|
2023-06-09 03:58:38 +00:00
|
|
|
writeFileSync(path, JSON.stringify(this.#data));
|
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
|
|
|
getApplicationConfig () {
|
2023-06-09 03:58:38 +00:00
|
|
|
return this.#data.application;
|
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
|
|
|
getResourceConfig () {
|
2023-06-09 03:58:38 +00:00
|
|
|
return this.#data.resources;
|
|
|
|
}
|
2023-06-29 21:35:19 +00:00
|
|
|
|
|
|
|
getUserConfig (username) {
|
2023-06-09 03:58:38 +00:00
|
|
|
if (this.#data.users[username]) {
|
|
|
|
return this.#data.users[username];
|
2023-06-29 21:35:19 +00:00
|
|
|
} else {
|
2023-06-09 03:58:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 21:35:19 +00:00
|
|
|
export const db = new LocalDB();
|
2023-06-09 03:58:38 +00:00
|
|
|
export const pveAPI = db.getApplicationConfig().pveAPI;
|
|
|
|
export const pveAPIToken = db.getApplicationConfig().pveAPIToken;
|
|
|
|
export const listenPort = db.getApplicationConfig().listenPort;
|
|
|
|
export const hostname = db.getApplicationConfig().hostname;
|
2023-06-29 21:35:19 +00:00
|
|
|
export const domain = db.getApplicationConfig().domain;
|