webapi-eca/coffee/config.coffee
Dominic Bosch 8c115aa71d What a wonderful detour over docco, only to recognize that it was always possible to auto generate documentation.
... if one would just define the correct folder...
at least deep knowledge about docco now, plus that it doesn't use glob (leaves it a user task...) while groc does use glob to resolve file paths, yay!
2013-11-20 15:41:41 +01:00

73 lines
1.7 KiB
CoffeeScript

'use strict'
path = require 'path'
log = require './logging'
fs = require 'fs'
config = null
###
Calling the module as a function will make it look for the `relPath` property in the
args object and then try to load a config file from that relative path.
@param {Object} args
###
exports = module.exports = (args) ->
args = args ? {}
log args
if typeof args.relPath is 'string'
loadConfigFiles args.relPath
module.exports
###
@Function loadConfigFile
Tries to load a configuration file from the path relative to this module's parent folder.
@param {String} relPath
###
loadConfigFile = (relPath) ->
try
### We read the config file synchronously from the file system and try to parse it ###
config = JSON.parse fs.readFileSync path.resolve __dirname, '..', relPath
if config and config.http_port and config.db_port and
config.crypto_key and config.session_secret
log.print 'CF', 'config file loaded successfully'
else
log.error 'CF', new Error("""Missing property in config file, requires:
- http_port
- db_port
- crypto_key
- session_secret""")
catch e
e.addInfo = 'no config ready'
log.error 'CF', e
loadConfigFile path.join('config', 'config.json')
### Answer true if the config file is ready, else false ###
exports.isReady = -> config?
###
Fetch a property from the configuration
@param {String} prop
###
fetchProp = (prop) -> config?[prop]
###
Get the HTTP port
###
exports.getHttpPort = -> fetchProp 'http_port'
###
Get the DB port
###
exports.getDBPort = -> fetchProp 'db_port'
###
Get the crypto key
###
exports.getCryptoKey = -> fetchProp 'crypto_key'
###
Get the session secret
###
exports.getSessionSecret = -> fetchProp 'session_secret'