wf-radio/src/utils/configManager.js

89 lines
2 KiB
JavaScript
Raw Normal View History

// --- Define constants ---
// Imports
const fs = require("fs");
const path = require("path");
const { version } = require("../../package.json");
// Var decalaration
const config_folder = "config";
const config_file_name = "config.json"
// Global variables
let config = {};
// --- Default config ---
const default_config = {
"port": 8000,
"users": {
"admin": {
"username": "admin",
"password": "admin"
}
},
"database": {
"type": "sqlite"
},
"auth" : {
2025-05-04 23:14:00 +02:00
"JWT_secret": "HGF7654EGBNKJNBJH6754356788GJHGY",
"tokenExpiry": "1h"
}
}
// --- 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
2025-05-04 23:14:00 +02:00
if (!user_config.auth || !user_config.auth.JWT_secret) {
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() {
return config;
}
async function getJWTSecret() {
2025-05-04 23:14:00 +02:00
return config.auth.JWT_secret || process.env.JWT_secret;
}
async function getVersion() {
// Could be done with process.env.npm_package_version
// but may not work without without npm
return version;
}
// Exports
module.exports = { loadConfig, getConfig, getVersion, getJWTSecret };