Skip to content

Commit ecb0533

Browse files
committed
refactor(config.js): Extract config file
* Separate different config source to each files * Freeze config object
1 parent 4738ba7 commit ecb0533

File tree

15 files changed

+767
-618
lines changed

15 files changed

+767
-618
lines changed

app.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,17 @@ var response = require('./lib/response')
2929
var models = require('./lib/models')
3030

3131
// generate front-end constants by template
32-
var configJson = config.raw
3332
var constpath = path.join(__dirname, './public/js/lib/common/constant.ejs')
34-
var googleApiKey = (fs.existsSync('/run/secrets/google_apiKey') && config.handleDockerSecret('google_apiKey')) || process.env.HMD_GOOGLE_API_KEY || (configJson.google && configJson.google.apiKey) || ''
35-
var googleClientID = (fs.existsSync('/run/secrets/google_clientID') && config.handleDockerSecret('google_clientID')) || process.env.HMD_GOOGLE_CLIENT_ID || (configJson.google && configJson.google.clientID) || ''
36-
var dropboxAppKey = (fs.existsSync('/run/secrets/dropbox_appKey') && config.handleDockerSecret('dropbox_appKey')) || process.env.HMD_DROPBOX_APP_KEY || (configJson.dropbox && configJson.dropbox.appKey) || ''
3733
var data = {
3834
domain: config.domain,
3935
urlpath: config.urlpath,
4036
debug: config.debug,
4137
version: config.version,
42-
GOOGLE_API_KEY: googleApiKey,
43-
GOOGLE_CLIENT_ID: googleClientID,
44-
DROPBOX_APP_KEY: dropboxAppKey
38+
GOOGLE_API_KEY: config.google.clientSecret,
39+
GOOGLE_CLIENT_ID: config.google.clientID,
40+
DROPBOX_APP_KEY: config.dropbox.clientSecret
4541
}
42+
4643
ejs.renderFile(constpath, data, {}, function (err, str) {
4744
if (err) throw new Error(err)
4845
fs.writeFileSync(path.join(__dirname, './public/build/constant.js'), str)
@@ -204,7 +201,7 @@ function startListen () {
204201
server.listen(config.port, function () {
205202
var schema = config.usessl ? 'HTTPS' : 'HTTP'
206203
logger.info('%s Server listening at port %d', schema, config.port)
207-
config.maintenance = false
204+
realtime.maintenance = false
208205
})
209206
}
210207

lib/config.js

Lines changed: 0 additions & 223 deletions
This file was deleted.

lib/config/default.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict'
2+
3+
module.exports = {
4+
domain: '',
5+
urlpath: '',
6+
port: 3000,
7+
urladdport: false,
8+
alloworigin: ['localhost'],
9+
usessl: false,
10+
protocolusessl: false,
11+
usecdn: true,
12+
allowanonymous: true,
13+
allowfreeurl: false,
14+
defaultpermission: 'editable',
15+
dburl: '',
16+
db: {},
17+
// ssl path
18+
sslkeypath: '',
19+
sslcertpath: '',
20+
sslcapath: '',
21+
dhparampath: '',
22+
// other path
23+
tmppath: './tmp',
24+
defaultnotepath: './public/default.md',
25+
docspath: './public/docs',
26+
indexpath: './public/views/index.ejs',
27+
hackmdpath: './public/views/hackmd.ejs',
28+
errorpath: './public/views/error.ejs',
29+
prettypath: './public/views/pretty.ejs',
30+
slidepath: './public/views/slide.ejs',
31+
// session
32+
sessionname: 'connect.sid',
33+
sessionsecret: 'secret',
34+
sessionlife: 14 * 24 * 60 * 60 * 1000, // 14 days
35+
staticcachetime: 1 * 24 * 60 * 60 * 1000, // 1 day
36+
// socket.io
37+
heartbeatinterval: 5000,
38+
heartbeattimeout: 10000,
39+
// document
40+
documentmaxlength: 100000,
41+
// image upload setting, available options are imgur/s3/filesystem
42+
imageUploadType: 'filesystem',
43+
imgur: {
44+
clientID: undefined
45+
},
46+
s3: {
47+
accessKeyId: undefined,
48+
secretAccessKey: undefined,
49+
region: undefined
50+
},
51+
s3bucket: undefined,
52+
// authentication
53+
facebook: {
54+
clientID: undefined,
55+
clientSecret: undefined
56+
},
57+
twitter: {
58+
consumerKey: undefined,
59+
consumerSecret: undefined
60+
},
61+
github: {
62+
clientID: undefined,
63+
clientSecret: undefined
64+
},
65+
gitlab: {
66+
baseURL: undefined,
67+
clientID: undefined,
68+
clientSecret: undefined,
69+
scope: undefined
70+
},
71+
dropbox: {
72+
clientID: undefined,
73+
clientSecret: undefined
74+
},
75+
google: {
76+
clientID: undefined,
77+
clientSecret: undefined
78+
},
79+
ldap: {
80+
providerName: undefined,
81+
url: undefined,
82+
bindDn: undefined,
83+
bindCredentials: undefined,
84+
tokenSecret: undefined,
85+
searchBase: undefined,
86+
searchFilter: undefined,
87+
searchAttributes: undefined,
88+
tlsca: undefined
89+
},
90+
email: true,
91+
allowemailregister: true
92+
}

lib/config/defaultSSL.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
5+
function getFile (path) {
6+
if (fs.existsSync(path)) {
7+
return path
8+
}
9+
return undefined
10+
}
11+
12+
module.exports = {
13+
sslkeypath: getFile('/run/secrets/key.pem'),
14+
sslcertpath: getFile('/run/secrets/cert.pem'),
15+
sslcapath: getFile('/run/secrets/ca.pem'),
16+
dhparampath: getFile('/run/secrets/dhparam.pem')
17+
}

0 commit comments

Comments
 (0)