diff options
author | Rafael Marçalo <raroma09@gmail.com> | 2022-11-05 13:51:05 +0000 |
---|---|---|
committer | Rafael Marçalo <raroma09@gmail.com> | 2022-11-05 13:51:05 +0000 |
commit | ba8c332375ebfad4969c6c432fd8ea8d1b7ada82 (patch) | |
tree | 4a5178056ced276d96aa4f3fcb4f6831e50171ba /server | |
parent | 2cb8f9157ee1a919368d6bb0d0543124c67b5a0a (diff) |
Some progress on working configs
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 |