@@ -21,6 +21,15 @@ CONFIG = require './config'
2121
2222log = require (' ./log' )(module )
2323
24+
25+ notFoundHandler = (req , res , next ) ->
26+ res .status 404
27+ if Accepts (req).type (' text/html' )
28+ return res .render ' 404' , {reqJSON : StringifySafe (req, null , 2 )}
29+ else
30+ return res .end ()
31+ next ()
32+
2433errorHandler = (err , req , res , next ) ->
2534 if typeof err is ' string'
2635 err = {' message' : err}
@@ -37,14 +46,6 @@ errorHandler = (err, req, res, next) ->
3746 else
3847 res .send StringifySafe err, null , 2
3948
40- notFoundHandler = (req , res ) ->
41- if Accepts (req).type (' text/html' )
42- return res .render ' 404' , {reqJSON : StringifySafe (req, null , 2 )}
43- else
44- res .end ()
45-
46-
47-
4849accessLogger = Morgan ' combined' , stream : Fs .createWriteStream (__dirname + ' /../data/logs/access.log' , {flags : ' a' })
4950accessLoggerDev = Morgan ' dev'
5051
@@ -107,30 +108,32 @@ class InfolisWebservice
107108 # Log access
108109 @app .use accessLogger
109110 @app .use accessLoggerDev
110- for controller in [
111- ' header'
112- ' restful'
113- ' schemo'
114- ' upload'
115- ' execute'
116- ' monitor'
117- ' ldf'
118- ' swagger'
119- ' json-import'
120- ' syntax-highlight'
121- ' play'
122- ]
123- do (controller ) =>
124- log .info " Setting up route #{ controller} "
125- require (" ./routes/#{ controller} " )(@app )
126- # root route
127- @app .get ' /' , (req , res , next ) ->
128- res .status 302
129- res .header ' Location' , ' http://infolis.github.io'
130- res .end ()
131- # Error handler
132- @app .use errorHandler
133- @app .use notFoundHandler
111+ controlers = [
112+ ' header'
113+ ' restful'
114+ ' schemo'
115+ ' upload'
116+ ' execute'
117+ ' monitor'
118+ ' ldf'
119+ ' swagger'
120+ ' json-import'
121+ ' syntax-highlight'
122+ ' play'
123+ ]
124+ Async .eachSeries controlers, (controller , done ) =>
125+ log .info " Setting up route #{ controller} "
126+ done null , require (" ./routes/#{ controller} " )(@app )
127+ , (err , next ) =>
128+ # root route
129+ @app .get ' /' , (req , res , next ) ->
130+ res .status 302
131+ res .header ' Location' , ' http://infolis.github.io'
132+ res .end ()
133+ # Error handler
134+ @app .use errorHandler
135+ # 404 handler
136+ @app .use notFoundHandler
134137
135138 startServer : () ->
136139 log .info " Setting up routes"
0 commit comments