config.js 1.65 KB
Newer Older
1 2 3 4 5
'use strict'

const fs = require('fs')
const yaml = require('js-yaml')
const _ = require('lodash')
6
const path = require('path')
NGPixel's avatar
NGPixel committed
7
const cfgHelper = require('../helpers/config')
8

9 10 11 12 13 14 15 16
/**
 * Load Application Configuration
 *
 * @param      {Object}  confPaths  Path to the configuration files
 * @return     {Object}  Application Configuration
 */
module.exports = (confPaths) => {
  confPaths = _.defaults(confPaths, {
17 18 19
    config: path.join(ROOTPATH, 'config.yml'),
    data: path.join(SERVERPATH, 'app/data.yml'),
    dataRegex: path.join(SERVERPATH, 'app/regex.js')
20 21 22 23 24 25
  })

  let appconfig = {}
  let appdata = {}

  try {
26
    appconfig = yaml.safeLoad(
NGPixel's avatar
NGPixel committed
27 28
      cfgHelper.parseConfigValue(
        fs.readFileSync(confPaths.config, 'utf8')
29 30
      )
    )
Bamieh's avatar
Bamieh committed
31

32
    appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
33
    appdata.regex = require(confPaths.dataRegex)
34
  } catch (ex) {
35
    console.error(ex)
36 37 38 39 40 41 42
    process.exit(1)
  }

  // Merge with defaults

  appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)

43
  // Check port
NGPixel's avatar
NGPixel committed
44 45 46 47 48

  if (appconfig.port < 1) {
    appconfig.port = process.env.PORT || 80
  }

49 50 51 52
  // Convert booleans

  appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')

53 54
  // List authentication strategies

55 56
  appconfig.authStrategies = {
    list: _.filter(appconfig.auth, ['enabled', true]),
NGPixel's avatar
NGPixel committed
57
    socialEnabled: (_.chain(appconfig.auth).omit(['local', 'ldap']).filter(['enabled', true]).value().length > 0)
58 59 60 61
  }
  if (appconfig.authStrategies.list.length < 1) {
    console.error(new Error('You must enable at least 1 authentication strategy!'))
    process.exit(1)
62 63 64 65 66 67 68
  }

  return {
    config: appconfig,
    data: appdata
  }
}