<commit_before>Express = require \express
_       = require \underscore
H       = require \./api/helper

cookie-opts =
  secret: process.env.WDTS_COOKIE_SECRET or \secret
  cookie:
    maxAge: 60m * 60s * 1000ms

module.exports = server = Express!
  ..set \port, process.env.PORT || 80
  ..use Express.favicon! if env = server.settings.env
  ..use Express.logger \dev if env in <[ development test staging production ]>
  ..use Express.compress! if env in <[ staging production ]>
  ..use Express.cookieParser!
  ..use Express.cookieSession cookie-opts
  ..use Express.bodyParser!
  ..use allow-cross-domain
  ..use server.router
  ..use Express.static "#{__dirname}/app"
  ..use log-error show-stack:yes if env in <[ development ]>
  ..use log-error show-stack:no  if env in <[ test staging production ]>
  ..use handle-error
  ..use Express.errorHandler! if env in <[ development ]>

# http://backbonetutorials.com/cross-domain-sessions/
function allow-cross-domain req, res, next then
  res.set \Access-Control-Allow-Credentials, true
  res.set \Access-Control-Allow-Headers    , 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
  res.set \Access-Control-Allow-Methods    , 'GET,POST,PUT,DELETE,OPTIONS'
  res.set \Access-Control-Allow-Origin     , req.headers.origin
  next!

function handle-error err, req, res, next then
  msg = switch
    | err instanceof H.ApiError    => err.message
    | err.name is \ValidationError => get-validation-msg err
    | otherwise                    => 'Internal server error, sorry! :('
  res.send 500, msg

function log-error opts then
  (err, req, res, next) ->
    msg = if err.name is \ValidationError then get-validation-msg err else err.message
    console.log if opts.show-stack and err.stack then err.stack else msg
    next err
<commit_msg>set static file cache-control header to 1 hour, in preparation for CloudFront origin change from S3 to AppFog<commit_after>Express = require \express
_       = require \underscore
H       = require \./api/helper

const ONE-HOUR = 60m * 60s * 1000ms

cookie-opts =
  secret: process.env.WDTS_COOKIE_SECRET or \secret
  cookie:
    maxAge: ONE-HOUR

#http://docs.aws.amazon.com/AmazonCloudFront/2010-11-01/DeveloperGuide/Expiration.html
static-opts =
  maxAge: ONE-HOUR

env = (server = Express!).settings.env
module.exports = server
  ..set \port, process.env.PORT || 80
  ..use Express.favicon!
  ..use Express.logger \dev if env in <[ development test staging production ]>
  ..use Express.compress! if env in <[ staging production ]>
  ..use Express.cookieParser!
  ..use Express.cookieSession cookie-opts
  ..use Express.bodyParser!
  ..use allow-cross-domain
  ..use server.router
  ..use Express.static "#{__dirname}/app", static-opts
  ..use log-error show-stack:yes if env in <[ development ]>
  ..use log-error show-stack:no  if env in <[ test staging production ]>
  ..use handle-error
  ..use Express.errorHandler! if env in <[ development ]>

# http://backbonetutorials.com/cross-domain-sessions/
function allow-cross-domain req, res, next then
  res.set \Access-Control-Allow-Credentials, true
  res.set \Access-Control-Allow-Headers    , 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
  res.set \Access-Control-Allow-Methods    , 'GET,POST,PUT,DELETE,OPTIONS'
  res.set \Access-Control-Allow-Origin     , req.headers.origin
  next!

function handle-error err, req, res, next then
  msg = switch
    | err instanceof H.ApiError    => err.message
    | err.name is \ValidationError => get-validation-msg err
    | otherwise                    => 'Internal server error, sorry! :('
  res.send 500, msg

function log-error opts then
  (err, req, res, next) ->
    msg = if err.name is \ValidationError then get-validation-msg err else err.message
    console.log if opts.show-stack and err.stack then err.stack else msg
    next err

{'fraction_non_alphanumeric': 0.12097585513078471, 'fraction_numerical': 0.009557344064386318, 'mean_word_length': 3.1643979057591625, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1236910', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1239, 'n_words': 360}