From 5d6081e34777c8b99a3e9f2ed42a6de0732a4ff3 Mon Sep 17 00:00:00 2001 From: Rafael Marçalo Date: Sat, 29 Oct 2022 14:51:03 +0100 Subject: Added config file handling functionality --- app.js | 9 +++------ server/js/config.js | 32 +++++++++++++++++++------------- server/js/paths.js | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index d87b6e0..4184d74 100644 --- a/app.js +++ b/app.js @@ -4,24 +4,21 @@ const bodyParser = require('body-parser'); const express = require('express'); const path = require('path'); +var config = require('./server/js/config'); // handling variables const app = express(); -const { ensureFolder, envPaths } = require('./server/js/paths'); -const loadConfig = require('./server/js/config').loadConfig; -const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); -const configPath = path.join(configFolder, 'config.json'); +const configPath = config.defaultConfigPath; const PORT = module.exports.PORT = process.env.PORT || 3000; -var config = require('./server/js/config').config; // loading configurations app.use(express.static(path.join(__dirname, '/public'))); app.use(bodyParser.urlencoded({extended: true})); -config = loadConfig(configPath); // set the view engine to ejs app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, '/views')); +config.loadConfig(configPath); // routes management const home = require("./routes/home"); diff --git a/server/js/config.js b/server/js/config.js index 077f928..b1e6836 100644 --- a/server/js/config.js +++ b/server/js/config.js @@ -1,28 +1,34 @@ "use strict"; +// Imports const fs = require('fs'); +const path = require('path'); +const { ensureFolder, envPaths } = require('./paths'); -const defaultConfig = { - 'settings': { - 'nsfw_content': false - } -}; -var config = {}; +// Handling Variables +const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay')); +const defaultConfigPath = path.join(configFolder, 'config.json'); +var loadedConfig = {}; -function createConfig(configPath, data) +function createConfig() { - fs.writeFileSync(configPath, JSON.stringify(data), 'utf-8'); + const defaultConfig = { + 'settings': { + 'nsfw_content': false + } + }; + + fs.writeFileSync(configPath, JSON.stringify(defaultConfig), 'utf-8'); } function loadConfig(configPath) { - if ( !fs.existsSync(configPath) ) + if (!fs.existsSync(configPath)) { createConfig(configPath, defaultConfig); } - - return JSON.parse(fs.readFileSync(configPath)); -} + this.loadedConfig = JSON.parse(fs.readFileSync(configPath)); +} -module.exports = {loadConfig, config}; \ No newline at end of file +module.exports = { loadConfig, defaultConfigPath, loadedConfig }; \ No newline at end of file diff --git a/server/js/paths.js b/server/js/paths.js index ee10807..3bf6824 100644 --- a/server/js/paths.js +++ b/server/js/paths.js @@ -28,7 +28,7 @@ const envPaths = { HOME: os.homedir(), DATA: process.env.APPDATA || ensureFolder(path.join(os.homedir(), 'AppData', 'Roaming')), CONFIG: process.env.APPDATA || ensureFolder(path.join(os.homedir(), 'AppData', 'Roaming')), - CACHE: process.env.TEMP || process.env.TMP || ensureFolder(path.join(process.env.LOCALAPPDATA, 'Temp')) + CACHE: process.env.TEMP || process.env.TMP || path.join(process.env.LOCALAPPDATA, 'Temp') || ensureFolder(path.join(os.homedir(), 'AppData', 'Local', 'Temp')) } } -- cgit v1.2.3