1 | async = require "async"
|
2 | path = require "path"
|
3 | fs = require "fs"
|
4 |
|
5 | class 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 |
|
22 |
|
23 | realPath = path.join(__dirname, "modules", mod)
|
24 | unless fs.existsSync "#{realPath}.coffee"
|
25 |
|
26 |
|
27 |
|
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 |
|
50 | module.exports = AppServerConfiguration
|