diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/js/config.js | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/server/js/config.js b/server/js/config.js index c21a246..f688bb7 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -8,19 +8,27 @@ const { ensureFolder, envPaths } = require('./paths'); // Handling Variables const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); const defaultConfigPath = path.join(configFolder, 'config.json'); +const defaultConfig = { + 'settings': { + 'nsfw_content': false, + 'port': undefined, + 'tor': { + 'host': undefined, + 'port': undefined + } + } +}; var loadedConfig = {}; -function createConfig() +// Functions +function createConfig(customConfig, configPath) { - const defaultConfig = { - 'settings': { - 'nsfw_content': false - } - }; + const path = configPath || defaultConfigPath; + const config = customConfig || defaultConfig; try { - fs.writeFileSync(configPath, JSON.stringify(defaultConfig), 'utf-8'); + fs.writeFileSync(path, JSON.stringify(config), 'utf-8'); } catch (err) @@ -31,14 +39,16 @@ function createConfig() function loadConfig(configPath) { - try + const path = configPath || defaultConfigPath; + + if (configPath && !fs.existsSync(path)) { - if (!fs.existsSync(configPath)) - { - createConfig(configPath, defaultConfig); - } + createConfig(defaultConfig, path); + } - this.loadedConfig = JSON.parse(fs.readFileSync(configPath)); + try + { + this.loadedConfig = JSON.parse(fs.readFileSync(path)); } catch (err) @@ -47,4 +57,4 @@ function loadConfig(configPath) } } -module.exports = { loadConfig, defaultConfigPath, loadedConfig };
\ No newline at end of file +module.exports = { loadConfig, defaultConfigPath, loadedConfig, createConfig };
\ No newline at end of file |