<commit_before>#
# Sets up intial project settings, middleware, mounted apps, and
# global configuration such as overriding Backbone.sync and
# populating sharify data
#

# Dependencies
require './sharify'
sharify = require 'sharify'
bucketAssets = require 'bucket-assets'
express = require 'express'
session = require 'cookie-session'
bodyParser = require 'body-parser'
cookieParser = require 'cookie-parser'
session = require 'cookie-session'
Backbone = require 'backbone'
path = require 'path'
fs = require 'fs'
forceSSL = require 'express-force-ssl'
setupEnv = require './env'
setupAuth = require './auth'
logger = require 'artsy-morgan'
RavenServer = require 'raven'
{ locals, helpers, ua, sameOrigin } = require '../middleware'
{ parse } = require 'url'
{ NODE_ENV, SESSION_SECRET, SENTRY_PRIVATE_DSN } = process.env

module.exports = (app) ->

  # Override Backbone to use server-side sync
  Backbone.sync = require 'backbone-super-sync'

  # Route to ping for system up
  app.get '/system/up', (req, res) ->
    res.send 200, { nodejs: true }

  # Error Reporting
  if SENTRY_PRIVATE_DSN
    RavenServer.config(SENTRY_PRIVATE_DSN).install()
    app.use RavenServer.requestHandler()

  # Mount generic middleware & run setup modules
  if 'production' is NODE_ENV or 'staging' is NODE_ENV
    app.set('forceSSLOptions', { trustXFPHeader: true }).use forceSSL
  app.use sharify
  setupEnv app
  app.use cookieParser()
  app.use bodyParser.json limit:'5mb', extended: true
  app.use bodyParser.urlencoded limit:'5mb', extended: true
  app.use logger
  app.use session
    secret: SESSION_SECRET
    key: 'positron.sess'
  app.use bucketAssets()
  setupAuth app
  app.use locals

  if SENTRY_PRIVATE_DSN
    app.use RavenServer.errorHandler()

  app.use helpers
  app.use ua
  app.use sameOrigin

  # Mount apps
  app.use require '../../apps/edit'
  app.use require '../../apps/settings'
  app.use require '../../apps/switch_channel'
  app.use require '../../apps/queue'
  app.use require '../../apps/articles_list'

  # Mount static middleware for sub apps, components, and project-wide
  fs.readdirSync(path.resolve __dirname, '../../apps').forEach (fld) ->
    app.use express.static(path.resolve __dirname, "../../apps/#{fld}/public")
  fs.readdirSync(path.resolve __dirname, '../../components').forEach (fld) ->
    app.use express.static(path.resolve __dirname, "../../components/#{fld}/public")
  app.use express.static(path.resolve __dirname, '../../public')

  # Error handler
  require('../../components/error/server') app<commit_msg>Move error handling to after mounting sub-apps<commit_after>#
# Sets up intial project settings, middleware, mounted apps, and
# global configuration such as overriding Backbone.sync and
# populating sharify data
#

# Dependencies
require './sharify'
sharify = require 'sharify'
bucketAssets = require 'bucket-assets'
express = require 'express'
session = require 'cookie-session'
bodyParser = require 'body-parser'
cookieParser = require 'cookie-parser'
session = require 'cookie-session'
Backbone = require 'backbone'
path = require 'path'
fs = require 'fs'
forceSSL = require 'express-force-ssl'
setupEnv = require './env'
setupAuth = require './auth'
logger = require 'artsy-morgan'
RavenServer = require 'raven'
{ locals, helpers, ua, sameOrigin } = require '../middleware'
{ parse } = require 'url'
{ NODE_ENV, SESSION_SECRET, SENTRY_PRIVATE_DSN } = process.env

module.exports = (app) ->

  # Configure Sentry
  if SENTRY_PRIVATE_DSN
    RavenServer.config(SENTRY_PRIVATE_DSN).install()
    app.use RavenServer.requestHandler()

  # Override Backbone to use server-side sync
  Backbone.sync = require 'backbone-super-sync'

  # Route to ping for system up
  app.get '/system/up', (req, res) ->
    res.send 200, { nodejs: true }

  # Mount generic middleware & run setup modules
  if 'production' is NODE_ENV or 'staging' is NODE_ENV
    app.set('forceSSLOptions', { trustXFPHeader: true }).use forceSSL
  app.use sharify
  setupEnv app
  app.use cookieParser()
  app.use bodyParser.json limit:'5mb', extended: true
  app.use bodyParser.urlencoded limit:'5mb', extended: true
  app.use logger
  app.use session
    secret: SESSION_SECRET
    key: 'positron.sess'
  app.use bucketAssets()
  setupAuth app
  app.use locals
  app.use helpers
  app.use ua
  app.use sameOrigin

  # Mount apps
  app.use require '../../apps/edit'
  app.use require '../../apps/settings'
  app.use require '../../apps/switch_channel'
  app.use require '../../apps/queue'
  app.use require '../../apps/articles_list'

  # Mount static middleware for sub apps, components, and project-wide
  fs.readdirSync(path.resolve __dirname, '../../apps').forEach (fld) ->
    app.use express.static(path.resolve __dirname, "../../apps/#{fld}/public")
  fs.readdirSync(path.resolve __dirname, '../../components').forEach (fld) ->
    app.use express.static(path.resolve __dirname, "../../components/#{fld}/public")
  app.use express.static(path.resolve __dirname, '../../public')

  # Error handler
  if SENTRY_PRIVATE_DSN
    app.use RavenServer.errorHandler()

  require('../../components/error/server') app
{'fraction_non_alphanumeric': 0.13516676418958457, 'fraction_numerical': 0.0019504583577140628, 'mean_word_length': 3.770232558139535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4376521', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1612, 'n_words': 449}