2016-04-20 10:03:55 +00:00
|
|
|
// external modules
|
2017-02-07 13:17:05 +00:00
|
|
|
var fs = require('fs');
|
2016-04-20 10:03:55 +00:00
|
|
|
var path = require('path');
|
2017-01-21 03:25:11 +00:00
|
|
|
var fs = require('fs');
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// configs
|
|
|
|
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'));
|
|
|
|
|
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-04 09:02:57 +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
|
2016-10-10 13:16:58 +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
|
|
|
|
|
|
|
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')
|
|
|
|
? 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
|
|
|
|
2016-10-23 14:42:47 +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
|
|
|
|
2016-12-21 13:36:54 +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
|
|
|
|
2016-12-16 07:38:05 +00:00
|
|
|
var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl;
|
|
|
|
|
2017-02-10 02:46:10 +00:00
|
|
|
var permissions = ['editable', 'limited', 'locked', 'protected', 'private'];
|
|
|
|
if (allowanonymous) {
|
|
|
|
permissions.unshift('freely');
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-15 09:22:38 +00:00
|
|
|
var dburl = process.env.HMD_DB_URL || process.env.DATABASE_URL || config.dburl;
|
2016-12-22 13:20:43 +00:00
|
|
|
var db = config.db || {};
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// ssl path
|
2017-01-21 03:25:11 +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
|
|
|
|
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';
|
2016-05-29 09:54:24 +00:00
|
|
|
var slidepath = config.slidepath || './public/views/slide.ejs';
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// session
|
|
|
|
var sessionname = config.sessionname || 'connect.sid';
|
2017-01-21 03:25:11 +00:00
|
|
|
var sessionsecret = handleDockerSecret('sessionsecret') || config.sessionsecret || 'secret';
|
2016-04-20 10:03:55 +00:00
|
|
|
var sessionlife = config.sessionlife || 14 * 24 * 60 * 60 * 1000; //14 days
|
|
|
|
|
|
|
|
// static files
|
|
|
|
var staticcachetime = config.staticcachetime || 1 * 24 * 60 * 60 * 1000; // 1 day
|
|
|
|
|
|
|
|
// socket.io
|
2016-10-23 13:42:27 +00:00
|
|
|
var heartbeatinterval = config.heartbeatinterval || 5000;
|
|
|
|
var heartbeattimeout = config.heartbeattimeout || 10000;
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
// document
|
|
|
|
var documentmaxlength = config.documentmaxlength || 100000;
|
|
|
|
|
2016-11-14 09:07:07 +00:00
|
|
|
// image upload setting, available options are imgur/s3/filesystem
|
2016-11-17 08:28:02 +00:00
|
|
|
var imageUploadType = process.env.HMD_IMAGE_UPLOAD_TYPE || config.imageUploadType || 'imgur';
|
2016-11-14 09:07:07 +00:00
|
|
|
|
2016-11-16 04:05:24 +00:00
|
|
|
config.s3 = config.s3 || {};
|
2016-11-16 06:44:35 +00:00
|
|
|
var s3 = {
|
2017-01-21 03:25:11 +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,
|
2016-11-17 08:28:02 +00:00
|
|
|
region: process.env.HMD_S3_REGION || config.s3.region
|
2016-11-16 06:44:35 +00:00
|
|
|
}
|
2016-11-17 08:28:02 +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-01-21 03:25:11 +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
|
2016-10-10 13:16:58 +00:00
|
|
|
} : config.facebook || false;
|
2017-01-21 03:25:11 +00:00
|
|
|
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
|
2016-10-10 13:16:58 +00:00
|
|
|
} : config.twitter || false;
|
2017-01-21 03:25:11 +00:00
|
|
|
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
|
2016-10-10 13:16:58 +00:00
|
|
|
} : config.github || false;
|
2017-01-21 03:25:11 +00:00
|
|
|
var gitlab = (process.env.HMD_GITLAB_CLIENTID && process.env.HMD_GITLAB_CLIENTSECRET || fs.existsSync('/run/secrets/gitlab_clientID') && fs.existsSync('/run/secrets/gitlab_clientSecret')) ? {
|
2016-10-10 13:16:58 +00:00
|
|
|
baseURL: process.env.HMD_GITLAB_BASEURL,
|
2017-01-21 03:25:11 +00:00
|
|
|
clientID: handleDockerSecret('gitlab_clientID') || process.env.HMD_GITLAB_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('gitlab_clientSecret') || process.env.HMD_GITLAB_CLIENTSECRET
|
2016-10-10 13:16:58 +00:00
|
|
|
} : 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-01-21 03:25:11 +00:00
|
|
|
clientID: handleDockerSecret('dropbox_clientID') || process.env.HMD_DROPBOX_CLIENTID,
|
|
|
|
clientSecret: handleDockerSecret('dropbox_clientSecret') || process.env.HMD_DROPBOX_CLIENTSECRET
|
2017-01-15 03:58:00 +00:00
|
|
|
} : (config.dropbox && config.dropbox.clientID && config.dropbox.clientSecret && config.dropbox) || false;
|
2017-03-03 01:22:35 +00:00
|
|
|
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
|
2017-01-15 03:58:00 +00:00
|
|
|
} : (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-02-07 14:31:38 +00:00
|
|
|
) ? {} : false);
|
2016-12-13 21:31:35 +00:00
|
|
|
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) {
|
|
|
|
var ca = {
|
2017-02-07 13:17:05 +00:00
|
|
|
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'));
|
|
|
|
}
|
|
|
|
ldap.tlsOptions.ca = results;
|
2016-12-14 10:49:33 +00:00
|
|
|
}
|
2017-01-09 11:49:23 +00:00
|
|
|
}
|
|
|
|
if (process.env.HMD_LDAP_PROVIDERNAME) {
|
|
|
|
ldap.providerName = process.env.HMD_LDAP_PROVIDERNAME;
|
2016-12-14 10:49:33 +00:00
|
|
|
}
|
2017-01-21 03:25:11 +00:00
|
|
|
var imgur = handleDockerSecret('imgur_clientid') || process.env.HMD_IMGUR_CLIENTID || config.imgur || false;
|
2016-12-20 02:47:37 +00:00
|
|
|
var email = process.env.HMD_EMAIL ? (process.env.HMD_EMAIL === 'true') : !!config.email;
|
2017-01-12 15:53:22 +00:00
|
|
|
var allowemailregister = process.env.HMD_ALLOW_EMAIL_REGISTER ? (process.env.HMD_ALLOW_EMAIL_REGISTER === 'true') : ((typeof config.allowemailregister === 'boolean') ? config.allowemailregister : true);
|
2016-04-20 10:03:55 +00:00
|
|
|
|
|
|
|
function getserverurl() {
|
2016-04-21 19:33:24 +00:00
|
|
|
var url = '';
|
|
|
|
if (domain) {
|
|
|
|
var protocol = protocolusessl ? 'https://' : 'http://';
|
|
|
|
url = protocol + domain;
|
|
|
|
if (urladdport && ((usessl && port != 443) || (!usessl && port != 80)))
|
|
|
|
url += ':' + port;
|
|
|
|
}
|
2016-04-20 10:03:55 +00:00
|
|
|
if (urlpath)
|
|
|
|
url += '/' + urlpath;
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2017-01-02 03:19:01 +00:00
|
|
|
var version = '0.5.0';
|
|
|
|
var minimumCompatibleVersion = '0.5.0';
|
2016-06-17 08:09:33 +00:00
|
|
|
var maintenance = true;
|
2016-04-20 10:03:55 +00:00
|
|
|
var cwd = path.join(__dirname, '..');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
version: version,
|
2016-06-17 08:31:36 +00:00
|
|
|
minimumCompatibleVersion: minimumCompatibleVersion,
|
2016-06-01 06:18:54 +00:00
|
|
|
maintenance: maintenance,
|
2016-04-20 10:03:55 +00:00
|
|
|
debug: debug,
|
|
|
|
urlpath: urlpath,
|
|
|
|
port: port,
|
|
|
|
alloworigin: alloworigin,
|
|
|
|
usessl: usessl,
|
|
|
|
serverurl: getserverurl(),
|
|
|
|
usecdn: usecdn,
|
2016-12-21 13:36:54 +00:00
|
|
|
allowanonymous: allowanonymous,
|
2016-12-16 07:38:05 +00:00
|
|
|
allowfreeurl: allowfreeurl,
|
2017-02-09 04:24:40 +00:00
|
|
|
defaultpermission: defaultpermission,
|
2016-12-22 13:20:43 +00:00
|
|
|
dburl: dburl,
|
2016-04-20 10:03:55 +00:00
|
|
|
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,
|
2016-05-09 22:31:49 +00:00
|
|
|
gitlab: gitlab,
|
2016-04-20 10:03:55 +00:00
|
|
|
dropbox: dropbox,
|
2016-05-21 14:48:00 +00:00
|
|
|
google: google,
|
2016-12-13 21:31:35 +00:00
|
|
|
ldap: ldap,
|
2016-11-14 09:07:07 +00:00
|
|
|
imgur: imgur,
|
2016-12-01 17:58:14 +00:00
|
|
|
email: email,
|
2017-01-12 03:25:58 +00:00
|
|
|
allowemailregister: allowemailregister,
|
2016-11-16 04:05:24 +00:00
|
|
|
imageUploadType: imageUploadType,
|
|
|
|
s3: s3,
|
|
|
|
s3bucket: s3bucket
|
2016-04-23 10:58:24 +00:00
|
|
|
};
|