Skip to content

Commit 448b006

Browse files
committed
Update to generate front-end constants on server startup
To avoid extra webpacking on changing configs and follow the 12 factor app
1 parent b07eeed commit 448b006

5 files changed

Lines changed: 34 additions & 12 deletions

File tree

app.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ var auth = require('./lib/auth.js')
2929
var response = require('./lib/response.js')
3030
var models = require('./lib/models')
3131

32+
// generate front-end constants by template
33+
var constpath = path.join(__dirname, './public/js/lib/common/constant.ejs')
34+
var data = {
35+
domain: config.domain,
36+
urlpath: config.urlpath,
37+
debug: config.debug,
38+
version: config.version,
39+
GOOGLE_API_KEY: config.google && config.google.GOOGLE_API_KEY,
40+
GOOGLE_CLIENT_ID: config.google && config.google.GOOGLE_CLIENT_ID,
41+
DROPBOX_APP_KEY: config.dropbox && config.google.DROPBOX_APP_KEY
42+
}
43+
ejs.renderFile(constpath, data, {}, function (err, str) {
44+
if (err) throw new Error(err)
45+
fs.writeFileSync(path.join(__dirname, './public/build/constant.js'), str)
46+
})
47+
3248
// server setup
3349
var app = express()
3450
var server = null

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ module.exports = {
173173
version: version,
174174
minimumCompatibleVersion: minimumCompatibleVersion,
175175
maintenance: maintenance,
176-
debug: debug,
176+
domain: domain,
177177
urlpath: urlpath,
178+
debug: debug,
178179
port: port,
179180
alloworigin: alloworigin,
180181
usessl: usessl,

public/js/lib/common/constant.ejs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
window.domain = '<%- domain %>'
2+
window.urlpath = '<%- urlpath %>'
3+
window.debug = <%- debug %>
4+
window.version = '<%- version %>'
5+
6+
window.GOOGLE_API_KEY = '<%- GOOGLE_API_KEY %>'
7+
window.GOOGLE_CLIENT_ID = '<%- GOOGLE_CLIENT_ID %>'
8+
window.DROPBOX_APP_KEY = '<%- DROPBOX_APP_KEY %>'

public/js/lib/config/index.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import configJson from '../../../../config.json' // root path json config
1+
export const GOOGLE_API_KEY = window.GOOGLE_API_KEY || ''
2+
export const GOOGLE_CLIENT_ID = window.GOOGLE_CLIENT_ID || ''
3+
export const DROPBOX_APP_KEY = window.DROPBOX_APP_KEY || ''
24

3-
const config = process.env.NODE_ENV === 'production' ? configJson.production : configJson.development
4-
5-
export const GOOGLE_API_KEY = (config.google && config.google.apiKey) || ''
6-
export const GOOGLE_CLIENT_ID = (config.google && config.google.clientID) || ''
7-
export const DROPBOX_APP_KEY = (config.dropbox && config.dropbox.appKey) || ''
8-
9-
export const domain = config.domain || '' // domain name
10-
export const urlpath = config.urlpath || '' // sub url path, like: www.example.com/<urlpath>
11-
export const debug = config.debug || false
5+
export const domain = window.domain || '' // domain name
6+
export const urlpath = window.urlpath || '' // sub url path, like: www.example.com/<urlpath>
7+
export const debug = window.debug || false
128

139
export const port = window.location.port
1410
export const serverurl = `${window.location.protocol}//${domain || window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`
1511
window.serverurl = serverurl
1612
export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1]
1713
export const noteurl = `${serverurl}/${noteid}`
1814

19-
export const version = '0.5.0'
15+
export const version = window.version

public/views/includes/scripts.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<script src="<%= webpackConfig.output.baseUrl %>/build/constant.js"></script>
12
<% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
23
<script src="<%= webpackConfig.output.baseUrl %><%= htmlWebpackPlugin.files.chunks[chunk].entry %>" defer></script>
34
<% } %>

0 commit comments

Comments
 (0)