<commit_before>
util = require 'util'
async = require 'async'
fs = require 'fs'
path = require 'path'
url = require 'url'
colors = require 'colors'
mime = require 'mime'

{logger, extend, stripExtension} = require './common'
{loadTemplates, ContentTree} = require './'

#{renderResource, renderPage} = require './renderer'

colorCode = (code) ->
  s = code.toString()
  switch Math.floor code / 100
    when 2
      return s.green
    when 4
      return s.yellow
    when 5
      return s.red
    else
      return s

setup = (options, callback) ->

  contentHandler = (request, response, callback) ->
    uri = url.parse(request.url).pathname.replace /(.+)\/$/, '$1'
    logger.verbose "contentHandler: #{ uri }"
    async.waterfall [
      (callback) ->
        async.parallel
          templates: async.apply loadTemplates, options.templates
          contents: async.apply ContentTree.fromDirectory, options.contents
        , callback
      (result, callback) ->
        {contents, templates} = result
        async.detect ContentTree.flatten(contents), (item, callback) ->
          callback (uri is item.url)
        , (result) ->
          if result
            result.render options.locals, contents, templates, (error, res) ->
              if error
                callback error
              else if res instanceof fs.ReadStream
                response.writeHead 200, 'Content-Type': mime.lookup(result.filename)
                util.pump res, response, (error) ->
                  callback error, 200
              else if res instanceof Buffer
                response.writeHead 200, 'Content-Type': mime.lookup(result.filename)
                response.write res
                response.end()
                callback null, 200
              else
                callback() # not handled
          else
            callback() # not handled
    ], callback

  requestHandler = (request, response) ->
    start = new Date()
    uri = url.parse(request.url).pathname
    async.waterfall [
      async.apply contentHandler, request, response
    ], (error, responseCode) ->
      if error or !responseCode?
        # request not handled or error
        responseCode = if error? then 500 else 404
        response.writeHead responseCode, 'Content-Type': 'text/plain'
        response.end if error? then error.message else '404 Not Found\n'
      delta = new Date() - start
      logger.info "#{ colorCode(responseCode) } #{ uri.bold } " + "#{ delta }ms".grey
<commit_msg>Log errors in preview server<commit_after>
util = require 'util'
async = require 'async'
fs = require 'fs'
path = require 'path'
url = require 'url'
colors = require 'colors'
mime = require 'mime'

{logger, extend, stripExtension} = require './common'
{loadTemplates, ContentTree} = require './'

#{renderResource, renderPage} = require './renderer'

colorCode = (code) ->
  s = code.toString()
  switch Math.floor code / 100
    when 2
      return s.green
    when 4
      return s.yellow
    when 5
      return s.red
    else
      return s

setup = (options, callback) ->

  contentHandler = (request, response, callback) ->
    uri = url.parse(request.url).pathname.replace /(.+)\/$/, '$1'
    logger.verbose "contentHandler: #{ uri }"
    async.waterfall [
      (callback) ->
        async.parallel
          templates: async.apply loadTemplates, options.templates
          contents: async.apply ContentTree.fromDirectory, options.contents
        , callback
      (result, callback) ->
        {contents, templates} = result
        async.detect ContentTree.flatten(contents), (item, callback) ->
          callback (uri is item.url)
        , (result) ->
          if result
            result.render options.locals, contents, templates, (error, res) ->
              if error
                callback error
              else if res instanceof fs.ReadStream
                response.writeHead 200, 'Content-Type': mime.lookup(result.filename)
                util.pump res, response, (error) ->
                  callback error, 200
              else if res instanceof Buffer
                response.writeHead 200, 'Content-Type': mime.lookup(result.filename)
                response.write res
                response.end()
                callback null, 200
              else
                callback() # not handled
          else
            callback() # not handled
    ], callback

  requestHandler = (request, response) ->
    start = new Date()
    uri = url.parse(request.url).pathname
    async.waterfall [
      async.apply contentHandler, request, response
    ], (error, responseCode) ->
      if error or !responseCode?
        # request not handled or error
        responseCode = if error? then 500 else 404
        response.writeHead responseCode, 'Content-Type': 'text/plain'
        response.end if error? then error.message else '404 Not Found\n'
      delta = new Date() - start
      logger.info "#{ colorCode(responseCode) } #{ uri.bold } " + "#{ delta }ms".grey
      if error
        logger.error error.message, error

{'fraction_non_alphanumeric': 0.10760998810939358, 'fraction_numerical': 0.01109789932619897, 'mean_word_length': 2.115432098765432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64910', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1320, 'n_words': 411}