<commit_before>http = require 'http'
https = require 'https'
_ = require 'lodash'
Promise = require 'when'
express = require 'express'
bodyParser = require 'body-parser'
cookieParser = require 'cookie-parser'
methodOverride = require 'method-override'
clumper = require 'clumper'
redis = require 'redis'

renderPageWithComponent = require './render/renderPageWithComponent'
cacheResponse = require './routing/cacheResponse'
requireAuthentication = require './routing/requireAuthentication'
pluginRoutes = require './routing/pluginRoutes'
Authorize = require './routing/authorize'
errorHandler = require './routing/errorHandler'
missingHandler = require './routing/missingHandler'

SetupWebsockets = require './websockets/setup'

module.exports = (System) ->
  app = express()
  server = null
  httpsServer = null
  websockets = null

  {ip, ports} = System.getService 'redis'
  #ip = '192.168.59.104' # tmp because mac->docker = :-3(
  port = ports['6379/tcp']
  redisClient = redis.createClient port, ip
  redisConfig =
    host: ip
    port: port

  addPluginRoutes = pluginRoutes System, redisClient, app

  requirejsPaths = System.getGlobal 'public.requirejs.paths'

  clumper.config.configure
    pathFilter: (pathname) ->
      return requirejsPaths[pathname] if requirejsPaths?[pathname]
      pathname

  Server =
    noRestart: true
    baseDir: System.baseDir
    redisConfig: redisConfig
    getMe: System.getMe

    globals:
      public:
        layout:
          admin: 'kerplunk-server:layout'
          public: 'kerplunk-server:layout'
        requirejs:
          paths:
            react: '/plugins/kerplunk-server/js/react.js'
            lodash: '/plugins/kerplunk-server/js/lodash.min.js'
            'route-matcher': '/plugins/kerplunk-server/js/ba-routematcher.js'
            'reactive-data': '/plugins/kerplunk-server/js/reactive-data.js'
            baconjs: '/plugins/kerplunk-server/js/Bacon'
            when: '/plugins/kerplunk-server/js/when.min.js'
          shim:
            baconjs:
              exports: 'Bacon'
            'route-matcher':
              exports: 'routeMatcher'

    init: (next) ->
      return next() if server?
      credentials = System.getCredentials()

      server = http.createServer app
      if credentials
        httpsServer = https.createServer credentials, app
      else
        httpsServer = null
        console.log 'no credentials provided = no ssl'

      dbPlugin = System.getPlugin 'kerplunk-database'

      console.log 'Setting up websockets'
      websockets = SetupWebsockets System, server, httpsServer
      websockets.primus[0].save "#{System.baseDir}/public/js/primus.js"

      app.use bodyParser.urlencoded extended: true
      app.use bodyParser.json()
      app.use (req, res, next) ->
        res.defaultLayout = if /(^\/admin\/)|(^\/admin$)/.test req.originalUrl
          'admin'
        else
          'public'
        next()<commit_msg>remove setup status logging<commit_after>http = require 'http'
https = require 'https'
_ = require 'lodash'
Promise = require 'when'
express = require 'express'
bodyParser = require 'body-parser'
cookieParser = require 'cookie-parser'
methodOverride = require 'method-override'
clumper = require 'clumper'
redis = require 'redis'

renderPageWithComponent = require './render/renderPageWithComponent'
cacheResponse = require './routing/cacheResponse'
requireAuthentication = require './routing/requireAuthentication'
pluginRoutes = require './routing/pluginRoutes'
Authorize = require './routing/authorize'
errorHandler = require './routing/errorHandler'
missingHandler = require './routing/missingHandler'

SetupWebsockets = require './websockets/setup'

module.exports = (System) ->
  app = express()
  server = null
  httpsServer = null
  websockets = null

  {ip, ports} = System.getService 'redis'
  #ip = '192.168.59.104' # tmp because mac->docker = :-3(
  port = ports['6379/tcp']
  redisClient = redis.createClient port, ip
  redisConfig =
    host: ip
    port: port

  addPluginRoutes = pluginRoutes System, redisClient, app

  requirejsPaths = System.getGlobal 'public.requirejs.paths'

  clumper.config.configure
    pathFilter: (pathname) ->
      return requirejsPaths[pathname] if requirejsPaths?[pathname]
      pathname

  Server =
    noRestart: true
    baseDir: System.baseDir
    redisConfig: redisConfig
    getMe: System.getMe

    globals:
      public:
        layout:
          admin: 'kerplunk-server:layout'
          public: 'kerplunk-server:layout'
        requirejs:
          paths:
            react: '/plugins/kerplunk-server/js/react.js'
            lodash: '/plugins/kerplunk-server/js/lodash.min.js'
            'route-matcher': '/plugins/kerplunk-server/js/ba-routematcher.js'
            'reactive-data': '/plugins/kerplunk-server/js/reactive-data.js'
            baconjs: '/plugins/kerplunk-server/js/Bacon'
            when: '/plugins/kerplunk-server/js/when.min.js'
          shim:
            baconjs:
              exports: 'Bacon'
            'route-matcher':
              exports: 'routeMatcher'

    init: (next) ->
      return next() if server?
      credentials = System.getCredentials()

      server = http.createServer app
      if credentials
        httpsServer = https.createServer credentials, app
      else
        httpsServer = null
        console.log 'no credentials provided = no ssl'

      dbPlugin = System.getPlugin 'kerplunk-database'

      websockets = SetupWebsockets System, server, httpsServer
      websockets.primus[0].save "#{System.baseDir}/public/js/primus.js"

      app.use bodyParser.urlencoded extended: true
      app.use bodyParser.json()
      app.use (req, res, next) ->
        res.defaultLayout = if /(^\/admin\/)|(^\/admin$)/.test req.originalUrl
          'admin'
        else
          'public'
        next()
{'fraction_non_alphanumeric': 0.11410432395332876, 'fraction_numerical': 0.005833905284831846, 'mean_word_length': 2.777705767984446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '185766', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1720, 'n_words': 388}