@@ -25,6 +25,8 @@ errors = require './server/commons/errors'
25
25
request = require ' request'
26
26
Promise = require ' bluebird'
27
27
Promise .promisifyAll (request, {multiArgs : true })
28
+ Promise .promisifyAll (fs)
29
+ wrap = require ' co-express'
28
30
codePlayTags = require ' ./server/lib/code-play-tags'
29
31
30
32
{countries } = require ' ./app/core/utils'
@@ -279,38 +281,50 @@ setupJavascript404s = (app) ->
279
281
res .status (404 ).send (' Wrong hash' )
280
282
)
281
283
284
+ mainTemplate = fs .readFileAsync (path .join (__dirname , ' app' , ' assets' , ' main.html' ), ' utf8' ).then (data) =>
285
+ data = data .replace ' "environmentTag"' , if config .isProduction then ' "production"' else ' "development"'
286
+ data = data .replace / shaTag/ g , config .buildInfo .sha
287
+ data = data .replace ' "serverConfigTag"' , ' <%= mandate %>'
288
+ data = data .replace ' "userObjectTag"' , ' <%= user %>'
289
+ data = data .replace ' "serverSessionTag"' , ' <%= session %>'
290
+ data = data .replace ' "featuresTag"' , ' <%= features %>'
291
+ data = data .replace ' <!-- CodePlay Tags -->' , ' <%= codeplay %>'
292
+ return _ .template data
293
+
294
+ renderMain = wrap (req , res ) ->
295
+ template = yield mainTemplate
296
+ user = if req .user then JSON .stringify (UserHandler .formatEntity (req, req .user )).replace (/ \/ / g , ' \\ /' ) else ' {}'
297
+
298
+
299
+ try
300
+ mandate = yield Mandate .findOne ({}).cache (5 * 60 * 1000 ).exec ()
301
+ configData = _ .omit mandate ? .toObject () or {}, ' _id'
302
+ catch err
303
+ log .error " Error getting mandate config: #{ err} "
304
+ configData = {}
305
+
306
+ configData .picoCTF = config .picoCTF
307
+ configData .production = config .isProduction
308
+ configData .codeNinjas = (req .hostname ? req .host ) is ' coco.code.ninja'
309
+ domainRegex = new RegExp (" (.*\. )?(#{ config .mainHostname } |#{ config .unsafeContentHostname } )" )
310
+ domainPrefix = req .host .match (domainRegex)? [1 ] or ' '
311
+ configData .fullUnsafeContentHostname = domainPrefix + config .unsafeContentHostname
312
+ configData .buildInfo = config .buildInfo
313
+
314
+ res .header ' Cache-Control' , ' no-cache, no-store, must-revalidate'
315
+ res .header ' Pragma' , ' no-cache'
316
+ res .header ' Expires' , 0
317
+ str = template
318
+ user : user
319
+ session : JSON .stringify (_ .pick (req .session ? {}, ' amActually' , ' featureMode' ))
320
+ features : JSON .stringify (req .features )
321
+ codeplay : if req .features .codePlay then codePlayTags else ' <!-- CodePlay Tags -->'
322
+ mandate : JSON .stringify configData
323
+ res .status (200 ).send str
282
324
283
325
setupFallbackRouteToIndex = (app ) ->
284
- app .all ' *' , (req , res ) ->
285
- fs .readFile path .join (__dirname , ' public' , ' main.html' ), ' utf8' , (err , data ) ->
286
- log .error " Error modifying main.html: #{ err} " if err
287
- # insert the user object directly into the html so the application can have it immediately. Sanitize </script>
288
- user = if req .user then JSON .stringify (UserHandler .formatEntity (req, req .user )).replace (/ \/ / g , ' \\ /' ) else ' {}'
289
-
290
- Mandate .findOne ({}).cache (5 * 60 * 1000 ).exec (err, mandate) ->
291
- if err
292
- log .error " Error getting mandate config: #{ err} "
293
- configData = {}
294
- else
295
- configData = _ .omit mandate ? .toObject () or {}, ' _id'
296
- configData .picoCTF = config .picoCTF
297
- configData .production = config .isProduction
298
- configData .codeNinjas = (req .hostname ? req .host ) is ' coco.code.ninja'
299
- domainRegex = new RegExp (" (.*\. )?(#{ config .mainHostname } |#{ config .unsafeContentHostname } )" )
300
- domainPrefix = req .host .match (domainRegex)? [1 ] or ' '
301
- configData .fullUnsafeContentHostname = domainPrefix + config .unsafeContentHostname
302
- configData .buildInfo = config .buildInfo
303
- data = data .replace ' "environmentTag"' , if config .isProduction then ' "production"' else ' "development"'
304
- data = data .replace / shaTag/ g , config .buildInfo .sha
305
- data = data .replace ' "serverConfigTag"' , JSON .stringify configData
306
- data = data .replace (' "userObjectTag"' , user)
307
- data = data .replace (' "serverSessionTag"' , JSON .stringify (_ .pick (req .session ? {}, ' amActually' , ' featureMode' )))
308
- data = data .replace (' "featuresTag"' , JSON .stringify (req .features ))
309
- data = data .replace (' <!-- CodePlay Tags -->' , codePlayTags) if req .features .codePlay
310
- res .header ' Cache-Control' , ' no-cache, no-store, must-revalidate'
311
- res .header ' Pragma' , ' no-cache'
312
- res .header ' Expires' , 0
313
- res .send 200 , data
326
+ app .all ' *' , renderMain
327
+
314
328
315
329
setupFacebookCrossDomainCommunicationRoute = (app ) ->
316
330
app .get ' /channel.html' , (req , res ) ->
0 commit comments