From 448b0061945b65dc36c08eaf6752060600d5895d Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 20 Mar 2017 01:39:09 +0800 Subject: Update to generate front-end constants on server startup To avoid extra webpacking on changing configs and follow the 12 factor app --- app.js | 16 ++++++++++++++++ lib/config.js | 3 ++- public/js/lib/common/constant.ejs | 8 ++++++++ public/js/lib/config/index.js | 18 +++++++----------- public/views/includes/scripts.ejs | 1 + 5 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 public/js/lib/common/constant.ejs diff --git a/app.js b/app.js index 722f0ead..f89b0718 100644 --- a/app.js +++ b/app.js @@ -29,6 +29,22 @@ var auth = require('./lib/auth.js') var response = require('./lib/response.js') var models = require('./lib/models') +// generate front-end constants by template +var constpath = path.join(__dirname, './public/js/lib/common/constant.ejs') +var data = { + domain: config.domain, + urlpath: config.urlpath, + debug: config.debug, + version: config.version, + GOOGLE_API_KEY: config.google && config.google.GOOGLE_API_KEY, + GOOGLE_CLIENT_ID: config.google && config.google.GOOGLE_CLIENT_ID, + DROPBOX_APP_KEY: config.dropbox && config.google.DROPBOX_APP_KEY +} +ejs.renderFile(constpath, data, {}, function (err, str) { + if (err) throw new Error(err) + fs.writeFileSync(path.join(__dirname, './public/build/constant.js'), str) +}) + // server setup var app = express() var server = null diff --git a/lib/config.js b/lib/config.js index 674dd113..cddfdda5 100644 --- a/lib/config.js +++ b/lib/config.js @@ -173,8 +173,9 @@ module.exports = { version: version, minimumCompatibleVersion: minimumCompatibleVersion, maintenance: maintenance, - debug: debug, + domain: domain, urlpath: urlpath, + debug: debug, port: port, alloworigin: alloworigin, usessl: usessl, diff --git a/public/js/lib/common/constant.ejs b/public/js/lib/common/constant.ejs new file mode 100644 index 00000000..e4a1cf0a --- /dev/null +++ b/public/js/lib/common/constant.ejs @@ -0,0 +1,8 @@ +window.domain = '<%- domain %>' +window.urlpath = '<%- urlpath %>' +window.debug = <%- debug %> +window.version = '<%- version %>' + +window.GOOGLE_API_KEY = '<%- GOOGLE_API_KEY %>' +window.GOOGLE_CLIENT_ID = '<%- GOOGLE_CLIENT_ID %>' +window.DROPBOX_APP_KEY = '<%- DROPBOX_APP_KEY %>' \ No newline at end of file diff --git a/public/js/lib/config/index.js b/public/js/lib/config/index.js index 1ea7a7ab..11e4389f 100644 --- a/public/js/lib/config/index.js +++ b/public/js/lib/config/index.js @@ -1,14 +1,10 @@ -import configJson from '../../../../config.json' // root path json config +export const GOOGLE_API_KEY = window.GOOGLE_API_KEY || '' +export const GOOGLE_CLIENT_ID = window.GOOGLE_CLIENT_ID || '' +export const DROPBOX_APP_KEY = window.DROPBOX_APP_KEY || '' -const config = process.env.NODE_ENV === 'production' ? configJson.production : configJson.development - -export const GOOGLE_API_KEY = (config.google && config.google.apiKey) || '' -export const GOOGLE_CLIENT_ID = (config.google && config.google.clientID) || '' -export const DROPBOX_APP_KEY = (config.dropbox && config.dropbox.appKey) || '' - -export const domain = config.domain || '' // domain name -export const urlpath = config.urlpath || '' // sub url path, like: www.example.com/ -export const debug = config.debug || false +export const domain = window.domain || '' // domain name +export const urlpath = window.urlpath || '' // sub url path, like: www.example.com/ +export const debug = window.debug || false export const port = window.location.port export const serverurl = `${window.location.protocol}//${domain || window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}` @@ -16,4 +12,4 @@ window.serverurl = serverurl export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1] export const noteurl = `${serverurl}/${noteid}` -export const version = '0.5.0' +export const version = window.version diff --git a/public/views/includes/scripts.ejs b/public/views/includes/scripts.ejs index 20d4003a..8766894a 100644 --- a/public/views/includes/scripts.ejs +++ b/public/views/includes/scripts.ejs @@ -1,3 +1,4 @@ + <% for (var chunk in htmlWebpackPlugin.files.chunks) { %> <% } %> -- cgit v1.2.3