UNPKG

1.97 kBtext/coffeescriptView Raw
1async = require "async"
2path = require "path"
3fs = require "fs"
4
5class AppServerConfiguration
6
7 @configFileName: "app.server.config.json"
8 @defaultConfigFilePath: path.resolve(__dirname, "../default.#{@configFileName}")
9
10 init: (@app) ->
11
12 configure: (callback) ->
13 configData = @loadConfiguration()
14
15 configureFunctions = []
16 for mod in configData
17 f = do(mod) =>
18 (callback) =>
19 error = null
20 try
21 # check if we are referencing a default module,
22 # by looking for it in the modules folder
23 realPath = path.join(__dirname, "modules", mod)
24 unless fs.existsSync "#{realPath}.coffee"
25 # otherwise we load the file specified
26 # based on the folder from where the
27 # process has been started
28 realPath = path.join(process.cwd(), mod)
29 c = require realPath
30 c = new c if typeof c is "function"
31 catch e
32 error = new Error("An error occured while loading App Server Module #{mod}")
33 return callback error, mod if error?
34 c.configure app: @app, callback: (err) ->
35 console.log "MiddleWare: #{mod} loaded"
36 callback(err, mod)
37 configureFunctions.push f
38 async.series configureFunctions, (err, results) ->
39 callback?(err)
40
41 loadConfiguration: ->
42 configData = []
43 try
44 configData = require path.join(process.cwd(), @constructor.configFileName)
45 catch e
46 console.log "#{@constructor.configFileName} not found"
47 configData = require @constructor.defaultConfigFilePath
48 configData
49
50module.exports = AppServerConfiguration