mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-23 10:16:32 -05:00
config/index.ts: Use const where possible, use new config type definition
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
1437cf3ea5
commit
9b323ba996
1 changed files with 41 additions and 36 deletions
|
@ -2,10 +2,17 @@ import crypto from 'crypto'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { merge } from 'lodash'
|
import { merge } from 'lodash'
|
||||||
import deepFreeze = require('deep-freeze')
|
|
||||||
import { Environment, Permission } from './enum'
|
import { Environment, Permission } from './enum'
|
||||||
import { logger } from '../logger'
|
import { logger } from '../logger'
|
||||||
import { getGitCommit, getGitHubURL } from './utils'
|
import { getGitCommit, getGitHubURL } from './utils'
|
||||||
|
import { defaultConfig } from './default'
|
||||||
|
import { defaultSSL } from './defaultSSL'
|
||||||
|
import { oldDefault } from './oldDefault'
|
||||||
|
import { oldEnvironment } from './oldEnvironment'
|
||||||
|
import { hackmdEnvironment } from './hackmdEnvironment'
|
||||||
|
import { environment } from './environment'
|
||||||
|
import { dockerSecret } from './dockerSecret'
|
||||||
|
import deepFreeze = require('deep-freeze')
|
||||||
|
|
||||||
const appRootPath = path.resolve(__dirname, '../../')
|
const appRootPath = path.resolve(__dirname, '../../')
|
||||||
const env = process.env.NODE_ENV || Environment.development
|
const env = process.env.NODE_ENV || Environment.development
|
||||||
|
@ -28,19 +35,17 @@ const packageConfig = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const configFilePath = path.resolve(appRootPath, process.env.CMD_CONFIG_FILE ||
|
const configFilePath = path.resolve(appRootPath, process.env.CMD_CONFIG_FILE ||
|
||||||
'config.json')
|
'config.json')
|
||||||
const fileConfig = fs.existsSync(configFilePath) ? require(configFilePath)[env] : undefined
|
const fileConfig = fs.existsSync(configFilePath) ? require(configFilePath)[env] : undefined
|
||||||
|
merge(defaultConfig, defaultSSL)
|
||||||
let defaultConfig = require('./default')
|
merge(defaultConfig, oldDefault)
|
||||||
merge(defaultConfig, require('./defaultSSL'))
|
|
||||||
merge(defaultConfig, require('./oldDefault'))
|
|
||||||
merge(defaultConfig, debugConfig)
|
merge(defaultConfig, debugConfig)
|
||||||
merge(defaultConfig, packageConfig)
|
merge(defaultConfig, packageConfig)
|
||||||
merge(defaultConfig, fileConfig)
|
merge(defaultConfig, fileConfig)
|
||||||
merge(defaultConfig, require('./oldEnvironment'))
|
merge(defaultConfig, oldEnvironment)
|
||||||
merge(defaultConfig, require('./hackmdEnvironment'))
|
merge(defaultConfig, hackmdEnvironment)
|
||||||
merge(defaultConfig, require('./environment'))
|
merge(defaultConfig, environment)
|
||||||
merge(defaultConfig, require('./dockerSecret'))
|
merge(defaultConfig, dockerSecret)
|
||||||
|
|
||||||
if (['debug', 'verbose', 'info', 'warn', 'error'].includes(defaultConfig.loglevel)) {
|
if (['debug', 'verbose', 'info', 'warn', 'error'].includes(defaultConfig.loglevel)) {
|
||||||
logger.level = defaultConfig.loglevel
|
logger.level = defaultConfig.loglevel
|
||||||
|
@ -49,15 +54,15 @@ if (['debug', 'verbose', 'info', 'warn', 'error'].includes(defaultConfig.logleve
|
||||||
}
|
}
|
||||||
|
|
||||||
// load LDAP CA
|
// load LDAP CA
|
||||||
if (defaultConfig.ldap.tlsca) {
|
if (defaultConfig.ldap?.tlsca) {
|
||||||
let ca = defaultConfig.ldap.tlsca.split(',')
|
const ca = defaultConfig.ldap.tlsca.split(',')
|
||||||
let caContent: string[] = []
|
const caContent: string[] = []
|
||||||
for (let i of ca) {
|
for (const i of ca) {
|
||||||
if (fs.existsSync(i)) {
|
if (fs.existsSync(i)) {
|
||||||
caContent.push(fs.readFileSync(i, 'utf8'))
|
caContent.push(fs.readFileSync(i, 'utf8'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let tlsOptions = {
|
const tlsOptions = {
|
||||||
ca: caContent
|
ca: caContent
|
||||||
}
|
}
|
||||||
defaultConfig.ldap.tlsOptions = defaultConfig.ldap.tlsOptions ? Object.assign(defaultConfig.ldap.tlsOptions, tlsOptions) : tlsOptions
|
defaultConfig.ldap.tlsOptions = defaultConfig.ldap.tlsOptions ? Object.assign(defaultConfig.ldap.tlsOptions, tlsOptions) : tlsOptions
|
||||||
|
@ -73,18 +78,18 @@ if (!(defaultConfig.defaultPermission in defaultConfig.permission)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache result, cannot change config in runtime!!!
|
// cache result, cannot change config in runtime!!!
|
||||||
defaultConfig.isStandardHTTPsPort = (function isStandardHTTPsPort () {
|
defaultConfig.isStandardHTTPsPort = (function isStandardHTTPsPort (): boolean {
|
||||||
return defaultConfig.useSSL && defaultConfig.port === 443
|
return defaultConfig.useSSL && defaultConfig.port === 443
|
||||||
})()
|
})()
|
||||||
defaultConfig.isStandardHTTPPort = (function isStandardHTTPPort () {
|
defaultConfig.isStandardHTTPPort = (function isStandardHTTPPort (): boolean {
|
||||||
return !defaultConfig.useSSL && defaultConfig.port === 80
|
return !defaultConfig.useSSL && defaultConfig.port === 80
|
||||||
})()
|
})()
|
||||||
|
|
||||||
// cache serverURL
|
// cache serverURL
|
||||||
defaultConfig.serverURL = (function getserverurl () {
|
defaultConfig.serverURL = (function getserverurl (): string {
|
||||||
var url = ''
|
let url = ''
|
||||||
if (defaultConfig.domain) {
|
if (defaultConfig.domain) {
|
||||||
var protocol = defaultConfig.protocolUseSSL ? 'https://' : 'http://'
|
const protocol = defaultConfig.protocolUseSSL ? 'https://' : 'http://'
|
||||||
url = protocol + defaultConfig.domain
|
url = protocol + defaultConfig.domain
|
||||||
if (defaultConfig.urlAddPort) {
|
if (defaultConfig.urlAddPort) {
|
||||||
if (!defaultConfig.isStandardHTTPPort || !defaultConfig.isStandardHTTPsPort) {
|
if (!defaultConfig.isStandardHTTPPort || !defaultConfig.isStandardHTTPsPort) {
|
||||||
|
@ -105,17 +110,17 @@ if (defaultConfig.serverURL === '') {
|
||||||
defaultConfig.Environment = Environment
|
defaultConfig.Environment = Environment
|
||||||
|
|
||||||
// auth method
|
// auth method
|
||||||
defaultConfig.isFacebookEnable = defaultConfig.facebook.clientID && defaultConfig.facebook.clientSecret
|
defaultConfig.isFacebookEnable = defaultConfig.facebook?.clientID && defaultConfig.facebook.clientSecret
|
||||||
defaultConfig.isGoogleEnable = defaultConfig.google.clientID && defaultConfig.google.clientSecret
|
defaultConfig.isGoogleEnable = defaultConfig.google?.clientID && defaultConfig.google.clientSecret
|
||||||
defaultConfig.isDropboxEnable = defaultConfig.dropbox.clientID && defaultConfig.dropbox.clientSecret
|
defaultConfig.isDropboxEnable = defaultConfig.dropbox?.clientID && defaultConfig.dropbox.clientSecret
|
||||||
defaultConfig.isTwitterEnable = defaultConfig.twitter.consumerKey && defaultConfig.twitter.consumerSecret
|
defaultConfig.isTwitterEnable = defaultConfig.twitter?.consumerKey && defaultConfig.twitter.consumerSecret
|
||||||
defaultConfig.isEmailEnable = defaultConfig.email
|
defaultConfig.isEmailEnable = defaultConfig.email
|
||||||
defaultConfig.isOpenIDEnable = defaultConfig.openID
|
defaultConfig.isOpenIDEnable = defaultConfig.openID
|
||||||
defaultConfig.isGitHubEnable = defaultConfig.github.clientID && defaultConfig.github.clientSecret
|
defaultConfig.isGitHubEnable = defaultConfig.github?.clientID && defaultConfig.github.clientSecret
|
||||||
defaultConfig.isGitLabEnable = defaultConfig.gitlab.clientID && defaultConfig.gitlab.clientSecret
|
defaultConfig.isGitLabEnable = defaultConfig.gitlab?.clientID && defaultConfig.gitlab.clientSecret
|
||||||
defaultConfig.isLDAPEnable = defaultConfig.ldap.url
|
defaultConfig.isLDAPEnable = defaultConfig.ldap?.url
|
||||||
defaultConfig.isSAMLEnable = defaultConfig.saml.idpSsoUrl
|
defaultConfig.isSAMLEnable = defaultConfig.saml?.idpSsoUrl
|
||||||
defaultConfig.isOAuth2Enable = defaultConfig.oauth2.clientID && defaultConfig.oauth2.clientSecret
|
defaultConfig.isOAuth2Enable = defaultConfig.oauth2?.clientID && defaultConfig.oauth2.clientSecret
|
||||||
|
|
||||||
// Check gitlab api version
|
// Check gitlab api version
|
||||||
if (defaultConfig.gitlab && defaultConfig.gitlab.version !== 'v4' && defaultConfig.gitlab.version !== 'v3') {
|
if (defaultConfig.gitlab && defaultConfig.gitlab.version !== 'v4' && defaultConfig.gitlab.version !== 'v3') {
|
||||||
|
@ -123,30 +128,30 @@ if (defaultConfig.gitlab && defaultConfig.gitlab.version !== 'v4' && defaultConf
|
||||||
defaultConfig.gitlab.version = 'v4'
|
defaultConfig.gitlab.version = 'v4'
|
||||||
}
|
}
|
||||||
// If gitlab scope is api, enable snippets Export/import
|
// If gitlab scope is api, enable snippets Export/import
|
||||||
defaultConfig.isGitlabSnippetsEnable = (!defaultConfig.gitlab.scope || defaultConfig.gitlab.scope === 'api') && defaultConfig.isGitLabEnable
|
defaultConfig.isGitlabSnippetsEnable = (!defaultConfig.gitlab?.scope || defaultConfig.gitlab.scope === 'api') && defaultConfig.isGitLabEnable
|
||||||
|
|
||||||
// Only update i18n files in development setups
|
// Only update i18n files in development setups
|
||||||
defaultConfig.updateI18nFiles = (env === Environment.development)
|
defaultConfig.updateI18nFiles = (env === Environment.development)
|
||||||
|
|
||||||
// merge legacy values
|
// merge legacy values
|
||||||
let keys = Object.keys(defaultConfig)
|
const keys = Object.keys(defaultConfig)
|
||||||
const uppercase = /[A-Z]/
|
const uppercase = /[A-Z]/
|
||||||
for (let i = keys.length; i--;) {
|
for (let i = keys.length; i--;) {
|
||||||
let lowercaseKey = keys[i].toLowerCase()
|
const lowercaseKey = keys[i].toLowerCase()
|
||||||
// if the config contains uppercase letters
|
// if the config contains uppercase letters
|
||||||
// and a lowercase version of this setting exists
|
// and a lowercase version of this setting exists
|
||||||
// and the config with uppercase is not set
|
// and the config with uppercase is not set
|
||||||
// we set the new config using the old key.
|
// we set the new config using the old key.
|
||||||
if (uppercase.test(keys[i]) &&
|
if (uppercase.test(keys[i]) &&
|
||||||
defaultConfig[lowercaseKey] !== undefined &&
|
defaultConfig[lowercaseKey] !== undefined &&
|
||||||
fileConfig[keys[i]] === undefined) {
|
fileConfig[keys[i]] === undefined) {
|
||||||
logger.warn('config.js contains deprecated lowercase setting for ' + keys[i] + '. Please change your config.js file to replace ' + lowercaseKey + ' with ' + keys[i])
|
logger.warn('config.js contains deprecated lowercase setting for ' + keys[i] + '. Please change your config.js file to replace ' + lowercaseKey + ' with ' + keys[i])
|
||||||
defaultConfig[keys[i]] = defaultConfig[lowercaseKey]
|
defaultConfig[keys[i]] = defaultConfig[lowercaseKey]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify users about the prefix change and inform them they use legacy prefix for environment variables
|
// Notify users about the prefix change and inform them they use legacy prefix for environment variables
|
||||||
if (Object.keys(process.env).toString().indexOf('HMD_') !== -1) {
|
if (Object.keys(process.env).toString().includes('HMD_')) {
|
||||||
logger.warn('Using legacy HMD prefix for environment variables. Please change your variables in future. For details see: https://github.com/codimd/server#environment-variables-will-overwrite-other-server-configs')
|
logger.warn('Using legacy HMD prefix for environment variables. Please change your variables in future. For details see: https://github.com/codimd/server#environment-variables-will-overwrite-other-server-configs')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +164,7 @@ if (defaultConfig.sessionSecret === 'secret') {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate upload upload providers
|
// Validate upload upload providers
|
||||||
if (['filesystem', 's3', 'minio', 'imgur', 'azure', 'lutim'].indexOf(defaultConfig.imageUploadType) === -1) {
|
if (!['filesystem', 's3', 'minio', 'imgur', 'azure', 'lutim'].includes(defaultConfig.imageUploadType)) {
|
||||||
logger.error('"imageuploadtype" is not correctly set. Please use "filesystem", "s3", "minio", "azure", "lutim" or "imgur". Defaulting to "filesystem"')
|
logger.error('"imageuploadtype" is not correctly set. Please use "filesystem", "s3", "minio", "azure", "lutim" or "imgur". Defaulting to "filesystem"')
|
||||||
defaultConfig.imageUploadType = 'filesystem'
|
defaultConfig.imageUploadType = 'filesystem'
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue