Updated config manager and app error utils (and main file accordingly), updated express version
This commit is contained in:
parent
575efff1cb
commit
72dbe04b3b
6 changed files with 264 additions and 323 deletions
|
|
@ -1,6 +1,7 @@
|
|||
const sqlite = require("better-sqlite3");
|
||||
|
||||
class SQLiteDatabase {
|
||||
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
this.db = null;
|
||||
|
|
@ -9,7 +10,7 @@ class SQLiteDatabase {
|
|||
async connect() {
|
||||
try {
|
||||
this.db = new sqlite("./data/sqlite.db");
|
||||
// db.pragma("journal_mode = WAL");
|
||||
// this.db.pragma("journal_mode = WAL");
|
||||
console.log("Connected to SQLite");
|
||||
} catch (err) {
|
||||
console.error("Error connecting to SQLite database: ", err);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
class AppError extends Error {
|
||||
constructor(statusCode, status = "", message) {
|
||||
constructor(statusCode, message, status = "", debugMsg = "") {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.debugMsg = debugMsg;
|
||||
// Get status
|
||||
if (status === "") {
|
||||
if (statusCode.toString().startsWith("4")) {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,73 @@
|
|||
// --- Define constants ---
|
||||
|
||||
// Imports
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { version } = require("../../package.json");
|
||||
|
||||
|
||||
// Var decalaration
|
||||
let config;
|
||||
const config_folder = "config";
|
||||
const config_file_name = "config.json"
|
||||
|
||||
// Load config
|
||||
config = JSON.parse(fs.readFileSync(path.join(config_folder, config_file_name)));
|
||||
// Global variables
|
||||
let config = {};
|
||||
|
||||
// --- Default config ---
|
||||
|
||||
const defaault_config = {
|
||||
|
||||
"port": 8000,
|
||||
|
||||
"users": {
|
||||
"admin": {
|
||||
"username": "admin",
|
||||
"password": "admin"
|
||||
}
|
||||
},
|
||||
|
||||
"database": {
|
||||
"type": "sqlite"
|
||||
},
|
||||
|
||||
"auth" : {
|
||||
"JWT_secret": "HGF7654EGBNKJNBJH6754356788GJHGY"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --- Functions ---
|
||||
|
||||
function loadConfig() {
|
||||
|
||||
let user_config;
|
||||
|
||||
// Parse
|
||||
try {
|
||||
// Get user config
|
||||
user_config = JSON.parse(fs.readFileSync(path.resolve(path.join(config_folder, config_file_name))));
|
||||
|
||||
// Warns
|
||||
if (!user_config.auth || !user_config.auth.jwtSecret) {
|
||||
console.warn("WARNING: No JWT secret provided, using the default one. Please note that using the default secret is a major security risk.")
|
||||
}
|
||||
|
||||
// Merge default and user configs (default values)
|
||||
config = { ...default_config, ...user_config };
|
||||
}
|
||||
catch (err) {
|
||||
// Error messages
|
||||
console.debug("Error:", err)
|
||||
console.error("Error loading configuration, using the default settings");
|
||||
console.debug("Search path:", path.resolve("./"));
|
||||
console.debug("Config file:", path.resolve(path.join(config_folder, config_file_name)))
|
||||
|
||||
config = default_config;
|
||||
}
|
||||
|
||||
return config;
|
||||
|
||||
}
|
||||
|
||||
|
||||
async function getConfig() {
|
||||
|
|
@ -15,10 +75,14 @@ async function getConfig() {
|
|||
}
|
||||
|
||||
async function getJWTSecret() {
|
||||
return config.JWT_Secret || process.env.JWT_Secret;
|
||||
return config.auth.JWT_Secret || process.env.JWT_Secret;
|
||||
}
|
||||
|
||||
// Default values
|
||||
async function getVersion() {
|
||||
// Could be done with process.env.npm_package_version
|
||||
// but may not work without without npm
|
||||
return version;
|
||||
}
|
||||
|
||||
// Exports
|
||||
module.exports = { getJWTSecret };
|
||||
module.exports = { loadConfig, getConfig, getVersion, getJWTSecret };
|
||||
Loading…
Add table
Add a link
Reference in a new issue