2016-04-20 10:03:55 +00:00
|
|
|
// external modules
|
2017-03-08 10:45:51 +00:00
|
|
|
var fs = require('fs')
|
|
|
|
var path = require('path')
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// configs
|
2017-03-08 10:45:51 +00:00
|
|
|
var env = process.env.NODE_ENV || 'development'
|
|
|
|
var config = require(path.join(__dirname, '..', 'config.json'))[env]
|
|
|
|
var debug = process.env.DEBUG ? (process.env.DEBUG === 'true') : ((typeof config.debug === 'boolean') ? config.debug : (env === 'development'))
|
2016-04-20 10:03:55 +00:00
|
|
|
|
2017-01-21 03:25:11 +00:00
|
|
|
// Create function that reads docker secrets but fails fast in case of a non docker environment
|
2017-03-08 10:45:51 +00:00
|
|
|
var handleDockerSecret = fs.existsSync('/run/secrets/') ? function (secret) {
|
|
|
|
return fs.existsSync('/run/secrets/' + secret) ? fs.readFileSync('/run/secrets/' + secret) : null
|
|
|
|
} : function () {
|
|
|
|
return null
|
|
|
|
}
|
2017-01-21 03:25:11 +00:00
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
// url
|
2017-03-08 10:45:51 +00:00
|
|
|
var domain = process.env.DOMAIN || process.env.HMD_DOMAIN || config.domain || ''
|
|
|
|
var urlpath = process.env.URL_PATH || process.env.HMD_URL_PATH || config.urlpath || ''
|
|
|
|
var port = process.env.PORT || process.env.HMD_PORT || config.port || 3000
|
|
|
|
var alloworigin = process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : (config.alloworigin || ['localhost'])
|
2016-04-20 10:03:55 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var usessl = !!config.usessl
|
2016-10-10 13:16:58 +00:00
|
|
|
var protocolusessl = (usessl === true && typeof process.env.HMD_PROTOCOL_USESSL === 'undefined' && typeof config.protocolusessl === 'undefined')
|
2017-03-08 10:45:51 +00:00
|
|
|
? true : (process.env.HMD_PROTOCOL_USESSL ? (process.env.HMD_PROTOCOL_USESSL === 'true') : !!config.protocolusessl)
|
|
|
|
var urladdport = process.env.HMD_URL_ADDPORT ? (process.env.HMD_URL_ADDPORT === 'true') : !!config.urladdport
|
2016-04-20 10:03:55 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var usecdn = process.env.HMD_USECDN ? (process.env.HMD_USECDN === 'true') : ((typeof config.usecdn === 'boolean') ? config.usecdn : true)
|
2016-04-20 10:03:55 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var allowanonymous = process.env.HMD_ALLOW_ANONYMOUS ? (process.env.HMD_ALLOW_ANONYMOUS === 'true') : ((typeof config.allowanonymous === 'boolean') ? config.allowanonymous : true)
|
2016-12-15 06:07:12 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl
|
2016-12-16 07:38:05 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var permissions = ['editable', 'limited', 'locked', 'protected', 'private']
|
2017-02-10 02:46:10 +00:00
|
|
|
if (allowanonymous) {
|
2017-03-08 10:45:51 +00:00
|
|
|
permissions.unshift('freely')
|
2017-02-10 02:46:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var defaultpermission = process.env.HMD_DEFAULT_PERMISSION || config.defaultpermission
|
|
|
|
defaultpermission = permissions.indexOf(defaultpermission) !== -1 ? defaultpermission : 'editable'
|
2017-02-09 04:24:40 +00:00
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
// db
|
2017-03-08 10:45:51 +00:00
|
|
|
var dburl = process.env.HMD_DB_URL || process.env.DATABASE_URL || config.dburl
|
|
|
|
var db = config.db || {}
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// ssl path
|
2017-03-08 10:45:51 +00:00
|
|
|
var sslkeypath = (fs.existsSync('/run/secrets/key.pem') ? '/run/secrets/key.pem' : null) || config.sslkeypath || ''
|
|
|
|
var sslcertpath = (fs.existsSync('/run/secrets/cert.pem') ? '/run/secrets/cert.pem' : null) || config.sslcertpath || ''
|
|
|
|
var sslcapath = (fs.existsSync('/run/secrets/ca.pem') ? '/run/secrets/ca.pem' : null) || config.sslcapath || ''
|
|
|
|
var dhparampath = (fs.existsSync('/run/secrets/dhparam.pem') ? '/run/secrets/dhparam.pem' : null) || config.dhparampath || ''
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// other path
|
2017-03-08 10:45:51 +00:00
|
|
|
var tmppath = config.tmppath || './tmp'
|
|
|
|
var defaultnotepath = config.defaultnotepath || './public/default.md'
|
|
|
|
var docspath = config.docspath || './public/docs'
|
|
|
|
var indexpath = config.indexpath || './public/views/index.ejs'
|
|
|
|
var hackmdpath = config.hackmdpath || './public/views/hackmd.ejs'
|
|
|
|
var errorpath = config.errorpath || './public/views/error.ejs'
|
|
|
|
var prettypath = config.prettypath || './public/views/pretty.ejs'
|
|
|
|
var slidepath = config.slidepath || './public/views/slide.ejs'
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// session
|
2017-03-08 10:45:51 +00:00
|
|
|
var sessionname = config.sessionname || 'connect.sid'
|
|
|
|
var sessionsecret = handleDockerSecret('sessionsecret') || config.sessionsecret || 'secret'
|
|
|
|
var sessionlife = config.sessionlife || 14 * 24 * 60 * 60 * 1000 // 14 days
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// static files
|
2017-03-08 10:45:51 +00:00
|
|
|
var staticcachetime = config.staticcachetime || 1 * 24 * 60 * 60 * 1000 // 1 day
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// socket.io
|
2017-03-08 10:45:51 +00:00
|
|
|
var heartbeatinterval = config.heartbeatinterval || 5000
|
|
|
|
var heartbeattimeout = config.heartbeattimeout || 10000
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// document
|
2017-03-08 10:45:51 +00:00
|
|
|
var documentmaxlength = config.documentmaxlength || 100000
|
2016-04-20 10:03:55 +00:00
|
|
|
|
2016-11-14 09:07:07 +00:00
|
|
|
// image upload setting, available options are imgur/s3/filesystem
|
2017-03-08 10:45:51 +00:00
|
|
|
var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur'
|
2016-11-14 09:07:07 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
config.s3 = config.s3 || {}
|
2016-11-16 06:44:35 +00:00
|
|
|
var s3 = {
|
2017-03-08 10:45:51 +00:00
|
|
|
accessKeyId: handleDockerSecret('s3_acccessKeyId') || process.env.HMD_S3_ACCESS_KEY_ID || config.s3.accessKeyId,
|
|
|
|
secretAccessKey: handleDockerSecret('s3_secretAccessKey') || process.env.HMD_S3_SECRET_ACCESS_KEY || config.s3.secretAccessKey,
|
|
|
|
region: process.env.HMD_S3_REGION || config.s3.region
|
2016-11-16 06:44:35 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
var s3bucket = process.env.HMD_S3_BUCKET || config.s3.bucket
|
2016-11-16 04:05:24 +00:00
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
// auth
|
2017-03-08 10:45:51 +00:00
|
|
|
var facebook = ((process.env.HMD_FACEBOOK_CLIENTID && process.env.HMD_FACEBOOK_CLIENTSECRET) || (fs.existsSync('/run/secrets/facebook_clientID') && fs.existsSync('/run/secrets/facebook_clientSecret'))) ? {
|
|
|
|
clientID: handleDockerSecret('facebook_clientID') || process.env.HMD_FACEBOOK_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('facebook_clientSecret') || process.env.HMD_FACEBOOK_CLIENTSECRET
|
|
|
|
} : config.facebook || false
|
|
|
|
var twitter = ((process.env.HMD_TWITTER_CONSUMERKEY && process.env.HMD_TWITTER_CONSUMERSECRET) || (fs.existsSync('/run/secrets/twitter_consumerKey') && fs.existsSync('/run/secrets/twitter_consumerSecret'))) ? {
|
|
|
|
consumerKey: handleDockerSecret('twitter_consumerKey') || process.env.HMD_TWITTER_CONSUMERKEY,
|
|
|
|
consumerSecret: handleDockerSecret('twitter_consumerSecret') || process.env.HMD_TWITTER_CONSUMERSECRET
|
|
|
|
} : config.twitter || false
|
|
|
|
var github = ((process.env.HMD_GITHUB_CLIENTID && process.env.HMD_GITHUB_CLIENTSECRET) || (fs.existsSync('/run/secrets/github_clientID') && fs.existsSync('/run/secrets/github_clientSecret'))) ? {
|
|
|
|
clientID: handleDockerSecret('github_clientID') || process.env.HMD_GITHUB_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('github_clientSecret') || process.env.HMD_GITHUB_CLIENTSECRET
|
|
|
|
} : config.github || false
|
|
|
|
var gitlab = ((process.env.HMD_GITLAB_CLIENTID && process.env.HMD_GITLAB_CLIENTSECRET) || (fs.existsSync('/run/secrets/gitlab_clientID') && fs.existsSync('/run/secrets/gitlab_clientSecret'))) ? {
|
|
|
|
baseURL: process.env.HMD_GITLAB_BASEURL,
|
|
|
|
clientID: handleDockerSecret('gitlab_clientID') || process.env.HMD_GITLAB_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('gitlab_clientSecret') || process.env.HMD_GITLAB_CLIENTSECRET
|
|
|
|
} : config.gitlab || false
|
2017-03-03 01:22:35 +00:00
|
|
|
var dropbox = ((process.env.HMD_DROPBOX_CLIENTID && process.env.HMD_DROPBOX_CLIENTSECRET) || (fs.existsSync('/run/secrets/dropbox_clientID') && fs.existsSync('/run/secrets/dropbox_clientSecret'))) ? {
|
2017-03-08 10:45:51 +00:00
|
|
|
clientID: handleDockerSecret('dropbox_clientID') || process.env.HMD_DROPBOX_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('dropbox_clientSecret') || process.env.HMD_DROPBOX_CLIENTSECRET
|
|
|
|
} : (config.dropbox && config.dropbox.clientID && config.dropbox.clientSecret && config.dropbox) || false
|
|
|
|
var google = ((process.env.HMD_GOOGLE_CLIENTID && process.env.HMD_GOOGLE_CLIENTSECRET) ||
|
|
|
|
(fs.existsSync('/run/secrets/google_clientID') && fs.existsSync('/run/secrets/google_clientSecret'))) ? {
|
|
|
|
clientID: handleDockerSecret('google_clientID') || process.env.HMD_GOOGLE_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('google_clientSecret') || process.env.HMD_GOOGLE_CLIENTSECRET
|
|
|
|
} : (config.google && config.google.clientID && config.google.clientSecret && config.google) || false
|
2017-02-07 14:31:38 +00:00
|
|
|
var ldap = config.ldap || ((
|
2016-12-13 21:31:35 +00:00
|
|
|
process.env.HMD_LDAP_URL ||
|
|
|
|
process.env.HMD_LDAP_BINDDN ||
|
|
|
|
process.env.HMD_LDAP_BINDCREDENTIALS ||
|
|
|
|
process.env.HMD_LDAP_TOKENSECRET ||
|
|
|
|
process.env.HMD_LDAP_SEARCHBASE ||
|
|
|
|
process.env.HMD_LDAP_SEARCHFILTER ||
|
2017-01-09 11:49:23 +00:00
|
|
|
process.env.HMD_LDAP_SEARCHATTRIBUTES ||
|
2017-02-07 14:31:38 +00:00
|
|
|
process.env.HMD_LDAP_TLS_CA ||
|
2017-01-09 11:49:23 +00:00
|
|
|
process.env.HMD_LDAP_PROVIDERNAME
|
2017-03-08 10:45:51 +00:00
|
|
|
) ? {} : false)
|
|
|
|
if (process.env.HMD_LDAP_URL) { ldap.url = process.env.HMD_LDAP_URL }
|
|
|
|
if (process.env.HMD_LDAP_BINDDN) { ldap.bindDn = process.env.HMD_LDAP_BINDDN }
|
|
|
|
if (process.env.HMD_LDAP_BINDCREDENTIALS) { ldap.bindCredentials = process.env.HMD_LDAP_BINDCREDENTIALS }
|
|
|
|
if (process.env.HMD_LDAP_TOKENSECRET) { ldap.tokenSecret = process.env.HMD_LDAP_TOKENSECRET }
|
|
|
|
if (process.env.HMD_LDAP_SEARCHBASE) { ldap.searchBase = process.env.HMD_LDAP_SEARCHBASE }
|
|
|
|
if (process.env.HMD_LDAP_SEARCHFILTER) { ldap.searchFilter = process.env.HMD_LDAP_SEARCHFILTER }
|
|
|
|
if (process.env.HMD_LDAP_SEARCHATTRIBUTES) { ldap.searchAttributes = process.env.HMD_LDAP_SEARCHATTRIBUTES }
|
2016-12-14 10:49:33 +00:00
|
|
|
if (process.env.HMD_LDAP_TLS_CA) {
|
2017-03-08 10:45:51 +00:00
|
|
|
var ca = {
|
|
|
|
ca: process.env.HMD_LDAP_TLS_CA.split(',')
|
|
|
|
}
|
|
|
|
ldap.tlsOptions = ldap.tlsOptions ? Object.assign(ldap.tlsOptions, ca) : ca
|
|
|
|
if (Array.isArray(ldap.tlsOptions.ca) && ldap.tlsOptions.ca.length > 0) {
|
|
|
|
var i, len, results
|
|
|
|
results = []
|
|
|
|
for (i = 0, len = ldap.tlsOptions.ca.length; i < len; i++) {
|
|
|
|
results.push(fs.readFileSync(ldap.tlsOptions.ca[i], 'utf8'))
|
2016-12-14 10:49:33 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
ldap.tlsOptions.ca = results
|
|
|
|
}
|
2017-01-09 11:49:23 +00:00
|
|
|
}
|
|
|
|
if (process.env.HMD_LDAP_PROVIDERNAME) {
|
2017-03-08 10:45:51 +00:00
|
|
|
ldap.providerName = process.env.HMD_LDAP_PROVIDERNAME
|
2016-12-14 10:49:33 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
var imgur = handleDockerSecret('imgur_clientid') || process.env.HMD_IMGUR_CLIENTID || config.imgur || false
|
|
|
|
var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email
|
|
|
|
var allowemailregister = process.env.HMD_ALLOW_EMAIL_REGISTER ? (process.env.HMD_ALLOW_EMAIL_REGISTER === 'true') : ((typeof config.allowemailregister === 'boolean') ? config.allowemailregister : true)
|
|
|
|
|
|
|
|
function getserverurl () {
|
|
|
|
var url = ''
|
|
|
|
if (domain) {
|
|
|
|
var protocol = protocolusessl ? 'https://' : 'http://'
|
|
|
|
url = protocol + domain
|
|
|
|
if (urladdport && ((usessl && port !== 443) || (!usessl && port !== 80))) { url += ':' + port }
|
|
|
|
}
|
|
|
|
if (urlpath) { url += '/' + urlpath }
|
|
|
|
return url
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var version = '0.5.0'
|
|
|
|
var minimumCompatibleVersion = '0.5.0'
|
|
|
|
var maintenance = true
|
|
|
|
var cwd = path.join(__dirname, '..')
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2017-03-08 10:45:51 +00:00
|
|
|
version: version,
|
|
|
|
minimumCompatibleVersion: minimumCompatibleVersion,
|
|
|
|
maintenance: maintenance,
|
|
|
|
debug: debug,
|
|
|
|
urlpath: urlpath,
|
|
|
|
port: port,
|
|
|
|
alloworigin: alloworigin,
|
|
|
|
usessl: usessl,
|
|
|
|
serverurl: getserverurl(),
|
|
|
|
usecdn: usecdn,
|
|
|
|
allowanonymous: allowanonymous,
|
|
|
|
allowfreeurl: allowfreeurl,
|
|
|
|
defaultpermission: defaultpermission,
|
|
|
|
dburl: dburl,
|
|
|
|
db: db,
|
|
|
|
sslkeypath: path.join(cwd, sslkeypath),
|
|
|
|
sslcertpath: path.join(cwd, sslcertpath),
|
|
|
|
sslcapath: path.join(cwd, sslcapath),
|
|
|
|
dhparampath: path.join(cwd, dhparampath),
|
|
|
|
tmppath: path.join(cwd, tmppath),
|
|
|
|
defaultnotepath: path.join(cwd, defaultnotepath),
|
|
|
|
docspath: path.join(cwd, docspath),
|
|
|
|
indexpath: path.join(cwd, indexpath),
|
|
|
|
hackmdpath: path.join(cwd, hackmdpath),
|
|
|
|
errorpath: path.join(cwd, errorpath),
|
|
|
|
prettypath: path.join(cwd, prettypath),
|
|
|
|
slidepath: path.join(cwd, slidepath),
|
|
|
|
sessionname: sessionname,
|
|
|
|
sessionsecret: sessionsecret,
|
|
|
|
sessionlife: sessionlife,
|
|
|
|
staticcachetime: staticcachetime,
|
|
|
|
heartbeatinterval: heartbeatinterval,
|
|
|
|
heartbeattimeout: heartbeattimeout,
|
|
|
|
documentmaxlength: documentmaxlength,
|
|
|
|
facebook: facebook,
|
|
|
|
twitter: twitter,
|
|
|
|
github: github,
|
|
|
|
gitlab: gitlab,
|
|
|
|
dropbox: dropbox,
|
|
|
|
google: google,
|
|
|
|
ldap: ldap,
|
|
|
|
imgur: imgur,
|
|
|
|
email: email,
|
|
|
|
allowemailregister: allowemailregister,
|
|
|
|
imageUploadType: imageUploadType,
|
|
|
|
s3: s3,
|
|
|
|
s3bucket: s3bucket
|
|
|
|
}
|