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 | |
parent | 2cb8f9157ee1a919368d6bb0d0543124c67b5a0a (diff) |
Some progress on working configs
-rw-r--r-- | app.js | 7 | ||||
-rw-r--r-- | routes/settings.js | 25 | ||||
-rw-r--r-- | server/js/config.js | 38 | ||||
-rw-r--r-- | views/settings.ejs | 46 |
4 files changed, 96 insertions, 20 deletions
@@ -9,7 +9,8 @@ var config = require('./server/js/config'); // handling variables const app = express(); const configPath = config.defaultConfigPath; -const PORT = module.exports.PORT = process.env.PORT || 3000; +config.loadConfig(configPath); +const PORT = module.exports.PORT = config.loadedConfig.settings.port || process.env.PORT || 3000; // loading configurations app.use(express.static(path.join(__dirname, '/public'))); @@ -18,19 +19,19 @@ app.use(bodyParser.urlencoded({extended: true})); // 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"); const info = require("./routes/info"); const search = require("./routes/search"); const visit = require("./routes/visit"); -const settings = require("./routes/settings"); +const {save, settings} = require("./routes/settings"); app.get('/', home); app.get('/info', info); app.get('/visit', visit); app.get('/settings', settings); +app.post('/settings', save); app.post('/search', search); // app start diff --git a/routes/settings.js b/routes/settings.js index 658c0f2..59b9f80 100644 --- a/routes/settings.js +++ b/routes/settings.js @@ -1,8 +1,29 @@ "use strict"; +const config = require("../server/js/config"); + function settings(req, res) { - res.render('settings', {showSearchBar: true}); + res.render('settings', {showSearchBar: true, config: config.loadedConfig}); +}; + +function save(req, res) +{ + let newConfig = { + 'settings': { + 'nsfw_content': ((req.body.nsfw === "on") ? true : false), + 'port': ((!isNaN(parseInt(req.body.port))) ? parseInt(req.body.port) : 3000), + 'tor': { + 'host': ((req.body.torHost) ? req.body.torHost : undefined), + 'port': ((!isNaN(parseInt(req.body.torPort))) ? parseInt(req.body.torPort) : undefined) + } + } + }; + + config.createConfig(newConfig); + config.loadConfig(); + + res.render('settings', {showSearchBar: true, config: config.loadedConfig}); }; -module.exports = settings; +module.exports = {settings, save}; 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 diff --git a/views/settings.ejs b/views/settings.ejs index 6919dc4..3538770 100644 --- a/views/settings.ejs +++ b/views/settings.ejs @@ -2,4 +2,48 @@ <h1>Settings</h1> -<%- include('partials/footer'); %> +<div class="text-center mt-5"> + <form action="settings" method="POST"> + <div name="content"> + <h2 class="display-6">Content</h2> + <div class="row justify-content-center"> + <div class="col-sm-4 mt-2"> + <input class="form-check-input" type="checkbox" name="nsfw" <% if (config.settings.nsfw_content) { %> checked="true" <% } %> id="nsfw"> + <label class="form-check-label" for="nsfw">NSFW Content</label> + </div> + </div> + </div> + + <div name="network" class="mt-5"> + <h2 class="display-6">Network</h2> + <div class="row justify-content-center"> + <div class="col-sm-4"> + <label class="form-check-label" for="port">Port Number</label> + <input type="text" class="text-bg-dark form-control w-50 m-auto mt-2 text-center" placeholder="3000" name="port" <% if (config.settings.port) { %> value="<%= config.settings.port %>" <% } %> id="port"> + </div> + <div class="col-sm-4"> + <label class="form-check-label" for="tor">Tor SOCKS5 Proxy</label> + <input type="text" class="text-bg-dark form-control w-50 m-auto mt-2 text-center" placeholder="host" name="torHost" <% if (config.settings.tor.host) { %> value="<%= config.settings.tor.host %>" <% } %> id="host"> + <input type="text" class="text-bg-dark form-control w-50 m-auto mt-2 text-center" placeholder="port" name="torPort" <% if (config.settings.tor.port) { %> value="<%= config.settings.tor.port %>" <% } %> id="port"> + </div> + </div> + </div> + + <div name="network" class="mt-5"> + <div class="row justify-content-center"> + <div class="col-sm-4"> + <button type="submit" class="btn btn-primary"> + <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" + viewBox="0 0 16 16"> + <path + d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"> + </svg> + Save + </button> + </div> + </div> + </div> + </form> +</div> + +<%- include('partials/footer'); %>
\ No newline at end of file |