From 66e06017267b70cb48c1fbe9592dd7e0a8bc4f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Lescano?= Date: Tue, 21 Apr 2015 14:36:33 -0300 Subject: [PATCH] Add client side new config --- Dockerfile | 2 +- bin/dos-config | 27 ------------------ config/defaults.json | 58 +++++++++++++++++++++------------------ lib/config/client.js | 12 ++++++++ lib/config/component.json | 8 ++++++ lib/config/config.js | 1 + lib/config/index.js | 13 +++++++-- lib/debug/index.js | 10 +++---- lib/law-api/index.js | 4 +-- lib/layout/index.js | 9 ++---- package.json | 2 +- 11 files changed, 73 insertions(+), 73 deletions(-) delete mode 100644 bin/dos-config create mode 100644 lib/config/client.js create mode 100644 lib/config/component.json create mode 100644 lib/config/config.js diff --git a/Dockerfile b/Dockerfile index acd6ae0f47..dbf2e88217 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,4 +17,4 @@ WORKDIR /usr/src/democracyos/app # If you change the port, remember to set the proper ENV var to match. EXPOSE 80 -CMD node ./bin/dos-config && node index.js \ No newline at end of file +CMD node index.js \ No newline at end of file diff --git a/bin/dos-config b/bin/dos-config deleted file mode 100644 index 6dc3e349ae..0000000000 --- a/bin/dos-config +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env node - -/** - * Module dependencies. - */ - -var fs = require('fs'); -var path = require('path'); -var resolve = path.resolve; -var log = require('debug')('democracyos:config'); - -// Little hack to include `NODE_PATH=.` -require('node-path')(module, [resolve('.')]); - -// Compile client's config file -var config = require('lib/config'); -var client = {}; - -config.client.forEach(function(k) { - client[k] = config[k]; -}); - -var clientConfig = JSON.stringify(client); - -log('Generating client config: ' + clientConfig); - -fs.writeFileSync(resolve('lib/config/client.js'), 'module.exports = ' + clientConfig); diff --git a/config/defaults.json b/config/defaults.json index 85633f93e8..b907056dc0 100644 --- a/config/defaults.json +++ b/config/defaults.json @@ -1,19 +1,43 @@ { + "locale" : "en", "protocol": "http", "host": "localhost", - "privatePort": 3000, "publicPort": 3000, + "privatePort": 3000, "mongoUrl": "mongodb://localhost/DemocracyOS-dev", + "mongoUsersUrl": "", + "secret": "Generate a secret token and paste it here.", + "deploymentId": "", + "deploymentDomain": "", "corsDomains": "", "signinUrl": "", "signupUrl": "", "settingsUrl": "", + "staff": [], + "auth": { + "basic": { + "username": "", + "password": "" + } + }, + "ssl": { + "serverKey": "server.key", + "serverCert": "server.crt", + "port": 443, + "redirect": "normal" + }, + "notifications": { + "url": "http://localhost:9001/api/events", + "token": 1234 + }, "client": [ "protocol", "host", "publicPort", "env", + "locale", "logo", + "logoMobile", "favicon", "organizationName", "organizationUrl", @@ -21,25 +45,19 @@ "googleAnalyticsTrackingId", "commentsPerPage", "faq", - "tos", "pp", + "tos", "glossary", "signinUrl", "signupUrl", "settingsUrl", "clientDebug", "homeLink", - "logoMobile", "usersWhitelist", + "deploymentDomain", "headerBackgroundColor", "headerFontColor" ], - "auth": { - "basic": { - "username": "", - "password": "" - } - }, "socialshare" : { "siteName" : "DemocracyOS", "siteDescription" : "DemocracyOS voting system", @@ -49,16 +67,12 @@ "username" : "@democracyos" } }, - "locale" : "en", "logo": "/lib/boot/images/logo.png", "logoMobile": "/lib/boot/images/logo.png", "favicon" : " /lib/boot/images/favicon.ico", "organizationName" : "DemocracyOS on GitHub", "organizationUrl" : "https://github.com/DemocracyOS/app", "learnMoreUrl": "http://www.democracyos.org", - "headerBackgroundColor": "", - "headerFontColor": "", - "staff": [], "googleAnalyticsTrackingId" : "", "rssEnabled" : true, "commentsPerPage": 0, @@ -67,20 +81,10 @@ "tos": false, "pp": false, "glossary": false, - "notifications": { - "url": "http://localhost:9001/api/events", - "token": 1234 - }, - "secret": "Just a sample secret string", - "ssl": { - "serverKey": "server.key", - "serverCert": "server.crt", - "port": 443, - "redirect": "normal" - }, - "clientDebug": "", - "googleAPIKey": "", "homeLink": "/", + "clientDebug": "", "usersWhitelist": false, - "deploymentDomain": "localhost" + "headerBackgroundColor": "", + "headerFontColor": "", + "googleAPIKey": "" } diff --git a/lib/config/client.js b/lib/config/client.js new file mode 100644 index 0000000000..7554b7fd52 --- /dev/null +++ b/lib/config/client.js @@ -0,0 +1,12 @@ +var config = require('lib/config'); +var client = {}; + +config.client.forEach(function(key){ + if (!config.hasOwnProperty(key)) { + throw new Error('Config key "' + key + '" non existent.'); + } + + client[key] = config[key]; +}); + +module.exports = client; \ No newline at end of file diff --git a/lib/config/component.json b/lib/config/component.json new file mode 100644 index 0000000000..05cb7bdd3a --- /dev/null +++ b/lib/config/component.json @@ -0,0 +1,8 @@ +{ + "name": "config", + "description": "Client config component", + "dependencies": {}, + "locals": [], + "scripts": [ "config.js" ], + "main": "config.js" +} diff --git a/lib/config/config.js b/lib/config/config.js new file mode 100644 index 0000000000..b43e53a382 --- /dev/null +++ b/lib/config/config.js @@ -0,0 +1 @@ +module.exports = window.config; diff --git a/lib/config/index.js b/lib/config/index.js index 596dcc75ef..44568232d5 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -21,6 +21,7 @@ var log = require('debug')('democracyos:config'); var resolve = require('path').resolve; var path = require('path'); +var fs = require('fs'); var typeOf = require('component-type'); var changeCase = require('change-case'); var cast = require('./cast-string'); @@ -60,7 +61,7 @@ function parse(val, key, scope){ } var local = get(localConfig, s); - if (local.hasOwnProperty(key)) { + if (local && local.hasOwnProperty(key)) { var newVal = local[key]; if (typeOf(val) !== typeOf(newVal)) { throw new Error('Invalid value for key "' + key + '" on "' + environment + '.json": ' + '". Should be "' + typeOf(val) + '".'); @@ -73,7 +74,7 @@ function parse(val, key, scope){ function get(obj, scope) { var c = obj; - if (scope) scope.forEach(function(k){ c = c[k] }); + if (scope) scope.forEach(function(k){ c = c ? c[k] : null; }); return c; } @@ -87,4 +88,12 @@ function forEach(obj, cb) { }); } +config.env = environment; + +if (!config.secret || config.secret === defaultConfig.secret) { + var crypto = require('crypto'); + var token = crypto.randomBytes(32).toString('hex'); + throw new Error('Must set a unique token for your app on the "secret" key of the configuration.\n\nHere\'s one just for you: "' + token + '".\n'); +} + module.exports = config; diff --git a/lib/debug/index.js b/lib/debug/index.js index 6a4c87282e..2d8fa4dc6d 100644 --- a/lib/debug/index.js +++ b/lib/debug/index.js @@ -9,14 +9,12 @@ var debug = require('debug'); * Initialize debug */ -if (config.clientDebug && true === config.clientDebug) { - debug.disable(); +debug.disable(); + +if (config.clientDebug === '*') { debug.enable('democracyos:*'); -} else if (config.clientDebug && 'string' === typeof config.clientDebug) { - debug.disable(); +} else if (config.clientDebug) { debug.enable(config.clientDebug); -} else { - debug.disable(); } module.exports = debug; diff --git a/lib/law-api/index.js b/lib/law-api/index.js index 0fa2b42f02..5b5ca7fba9 100644 --- a/lib/law-api/index.js +++ b/lib/law-api/index.js @@ -399,8 +399,8 @@ app.post('/law/:id/publish', restrict, staff, function (req, res) { instance: instanceUrl }; - if (config('deployment id')) { - data.deploymentId = config('deployment id'); + if (config.deploymentId) { + data.deploymentId = config.deploymentId; } notifier.notify(eventName) diff --git a/lib/layout/index.js b/lib/layout/index.js index 9188b8ff2b..5e9d7c8774 100644 --- a/lib/layout/index.js +++ b/lib/layout/index.js @@ -3,16 +3,11 @@ */ var config = require('lib/config'); +var clientConfig = require('lib/config/client'); var path = require('path'); var resolve = path.resolve; var html = resolve(__dirname, 'index.jade'); module.exports = function (req, res) { - var client = {}; - - config.client.forEach(function(k) { - client[k] = config[k]; - }); - - res.render(html, { config: config, client: client }); + res.render(html, { config: config, client: clientConfig }); }; diff --git a/package.json b/package.json index 4396ed93e5..a0bde50ccb 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,6 @@ "node": "0.10.x" }, "scripts": { - "postinstall": "node ./bin/dos-install && node ./bin/dos-config && node ./bin/dos-build" + "postinstall": "node ./bin/dos-install && node ./bin/dos-build" } }