2025-04-24 18:28:00 +02:00
|
|
|
// --- Define constants ---
|
|
|
|
|
|
|
|
// Imports
|
2025-03-31 17:00:28 +02:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2025-04-24 18:28:00 +02:00
|
|
|
const { version } = require("../../package.json");
|
|
|
|
|
2025-03-31 17:00:28 +02:00
|
|
|
|
|
|
|
// Var decalaration
|
|
|
|
const config_folder = "config";
|
|
|
|
const config_file_name = "config.json"
|
|
|
|
|
2025-04-24 18:28:00 +02:00
|
|
|
// Global variables
|
|
|
|
let config = {};
|
|
|
|
|
|
|
|
// --- Default config ---
|
|
|
|
|
2025-05-04 19:31:03 +02:00
|
|
|
const default_config = {
|
2025-04-24 18:28:00 +02:00
|
|
|
|
|
|
|
"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;
|
|
|
|
|
|
|
|
}
|
2025-03-31 17:00:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
async function getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getJWTSecret() {
|
2025-04-24 18:28:00 +02:00
|
|
|
return config.auth.JWT_Secret || process.env.JWT_Secret;
|
2025-03-31 17:00:28 +02:00
|
|
|
}
|
|
|
|
|
2025-04-24 18:28:00 +02:00
|
|
|
async function getVersion() {
|
|
|
|
// Could be done with process.env.npm_package_version
|
|
|
|
// but may not work without without npm
|
|
|
|
return version;
|
|
|
|
}
|
2025-03-31 17:00:28 +02:00
|
|
|
|
|
|
|
// Exports
|
2025-04-24 18:28:00 +02:00
|
|
|
module.exports = { loadConfig, getConfig, getVersion, getJWTSecret };
|