diff --git a/config/.gitignore b/config/.gitignore new file mode 100644 index 0000000..78e3a98 --- /dev/null +++ b/config/.gitignore @@ -0,0 +1,2 @@ +*.json +!defaults.json diff --git a/config/defaults.json b/config/defaults.json new file mode 100644 index 0000000..fe416ff --- /dev/null +++ b/config/defaults.json @@ -0,0 +1,4 @@ +{ + "port": 3000, + "client": [] +} diff --git a/lib/config/client.js b/lib/config/client.js new file mode 100644 index 0000000..90761c9 --- /dev/null +++ b/lib/config/client.js @@ -0,0 +1,12 @@ +const config = require('.') +const 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 diff --git a/lib/config/config.js b/lib/config/config.js new file mode 100644 index 0000000..894d7bf --- /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 new file mode 100644 index 0000000..798b27b --- /dev/null +++ b/lib/config/index.js @@ -0,0 +1,6 @@ +const path = require('path') +const democracyosConfig = require('democracyos-config') + +module.exports = democracyosConfig({ + path: path.join(__dirname, '..', '..', 'config') +}) diff --git a/lib/server/index.js b/lib/server/index.js index c312354..f45ce1e 100644 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -1,7 +1,10 @@ const path = require('path') const express = require('express') -const app = express() const cookieParser = require('cookie-parser') +const config = require('../config') +const clientConfig = require('../config/client') + +const app = express() // Initialize database models require('./models')(app) @@ -33,8 +36,13 @@ app.use(require('webpack-hot-middleware')(compiler)) app.use('/api', require('./api/apps')) +const clientConfigJson = JSON.stringify(clientConfig) + app.get('/', language, initialState(db), (req, res) => { - res.render('home', { initialState: JSON.stringify(req.store.getState()) }) + res.render('home', { + initialState: JSON.stringify(req.store.getState()), + clientConfig: clientConfigJson + }) }) -app.listen(3000) +app.listen(config.port) diff --git a/lib/server/templates/home.pug b/lib/server/templates/home.pug index ad1d381..b4c125e 100644 --- a/lib/server/templates/home.pug +++ b/lib/server/templates/home.pug @@ -11,6 +11,7 @@ html #react-root=reactOutput script(type="text/javascript"). window.__INITIAL_STATE__ = !{initialState} + window.config = !{clientConfig} script(type="text/javascript", src="/dist/vendors.js") script(type="text/javascript", src="/dist/home.js") diff --git a/package.json b/package.json index 31d426e..130b2ca 100644 --- a/package.json +++ b/package.json @@ -70,12 +70,6 @@ "type-component": "0.0.1", "webpack": "1.14.0" }, - "scripts": { - "start": "node .", - "debug": "node debug ./lib/server", - "build": "webpack -p --colors --config webpack.config.babel.js", - "lint": "eslint ." - }, "devDependencies": { "babel-eslint": "^7.1.1", "eslint": "3.14.0", @@ -88,5 +82,14 @@ "react-transform-hmr": "^1.0.4", "webpack-dev-middleware": "1.9.0", "webpack-hot-middleware": "2.15.0" + }, + "browser": { + "lib/config": "lib/config/config" + }, + "scripts": { + "start": "node .", + "debug": "node debug ./lib/server", + "build": "webpack -p --colors --config webpack.config.babel.js", + "lint": "eslint ." } }