<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Only `require()` the newrelic module if explicity enabled.
// If required, modules will be instrumented.
require('../lib/newrelic')()

var config = require('../config').getProperties()
var jwtool = require('fxa-jwtool')

var log = require('../lib/log')(config.log.level)
var getGeoData = require('../lib/geodb')(log)

function main() {
  var metricsContext = require('../lib/metrics/context')(log, config)
  log.setMetricsContext(metricsContext)

  // Force the geo to load and run at startup, not waiting for it to run on
  // some route later.
  var knownIp = '192.168.3.11' // Mozilla MTV
  getGeoData(knownIp)
    .then(function(result) {
      log.info({ op: 'geodb.check', result: result })
    })

  process.stdout.write(JSON.stringify({
    event: 'config',
    data: config
  }) + '\n')

  if (config.env !== 'prod') {
    log.info(config, 'starting config')
  }

  var error = require('../lib/error')
  var Token = require('../lib/tokens')(log, config)
  var Password = require('../lib/crypto/password')(log, config)

  var signer = require('../lib/signer')(config.secretKeyFile, config.domain)
  var serverPublicKeys = {
    primary: jwtool.JWK.fromFile(
      config.publicKeyFile,
      {
        algorithm: 'RS',
        use: 'sig',
        kty: 'RSA'
      }
    ),
    secondary: config.oldPublicKeyFile ?
      jwtool.JWK.fromFile(
        config.oldPublicKeyFile,
        {
          algorithm: 'RS',
          use: 'sig',
          kty: 'RSA'
        }
      )
      : null
  }

  var Customs = require('../lib/customs')(log, error)

  var Server = require('../lib/server')
  var server = null
  var mailer = null
  var statsInterval = null
  var database = null
  var customs = null

  function logStatInfo() {
    log.stat(server.stat())
    log.stat(Password.stat())
  }

  require('../lib/mailer')(config, log)
    .done(
      function(m) {
        mailer = m

        var DB = require('../lib/db')(
          config,
          log,
          error,
          Token.SessionToken,
          Token.KeyFetchToken,
          Token.AccountResetToken,
          Token.PasswordForgotToken,
          Token.PasswordChangeToken
        )

        DB.connect(config[config.db.backend])
          .done(
            function (db) {
              database = db
              customs = new Customs(config.customsUrl)
              var routes = require('../lib/routes')(
                log,
                error,
                serverPublicKeys,
                signer,
                db,
                mailer,
                Password,
                config,
                customs,
                metricsContext
              )
              server = Server.create(log, error, config, routes, db)

              server.start(
                function (err) {
                  if (err) {
                    log.error({ op: 'server.start.1', msg: 'failed startup with error',
                                err: { message: err.message } })
                    process.exit(1)
                  } else {
                    log.info({ op: 'server.start.1', msg: 'running on ' + server.info.uri })
                  }
                }
              )
              statsInterval = setInterval(logStatInfo, 15000)
            },
            function (err) {
              log.error({ op: 'DB.connect', err: { message: err.message } })
              process.exit(1)
            }
          )

      }
    )

  process.on(
    'uncaughtException',
    function (err) {
      log.fatal(err)
      process.exit(8)
    }
  )
  process.on('SIGINT', shutdown)
  log.on('error', shutdown)

  function shutdown() {
    log.info({ op: 'shutdown' })
    clearInterval(statsInterval)
    server.stop(
      function () {
        customs.close()
        try {
          mailer.stop()
        } catch (e) {
          // XXX: simplesmtp module may quit early and set socket to `false`, stopping it may fail
          log.warn({ op: 'shutdown', message: 'Mailer client already disconnected' })
        }
        database.close()
        process.exit() //XXX: because of openid dep ಠ_ಠ
      }
    )
  }
}

main()
<commit_msg>fix(scripts): nicely stringify regexps when logging config<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Only `require()` the newrelic module if explicity enabled.
// If required, modules will be instrumented.
require('../lib/newrelic')()

var config = require('../config').getProperties()
var jwtool = require('fxa-jwtool')

var log = require('../lib/log')(config.log.level)
var getGeoData = require('../lib/geodb')(log)

function main() {
  var metricsContext = require('../lib/metrics/context')(log, config)
  log.setMetricsContext(metricsContext)

  // Force the geo to load and run at startup, not waiting for it to run on
  // some route later.
  var knownIp = '192.168.3.11' // Mozilla MTV
  getGeoData(knownIp)
    .then(function(result) {
      log.info({ op: 'geodb.check', result: result })
    })

  // RegExp instances serialise to empty objects, display regex strings instead.
  const stringifiedConfig =
    JSON.stringify(config, (k, v) =>
      v && v.constructor === RegExp ? v.toString() : v
    )

  process.stdout.write('{"event":"config","data":' + stringifiedConfig + '}\n')

  if (config.env !== 'prod') {
    log.info(stringifiedConfig, 'starting config')
  }

  var error = require('../lib/error')
  var Token = require('../lib/tokens')(log, config)
  var Password = require('../lib/crypto/password')(log, config)

  var signer = require('../lib/signer')(config.secretKeyFile, config.domain)
  var serverPublicKeys = {
    primary: jwtool.JWK.fromFile(
      config.publicKeyFile,
      {
        algorithm: 'RS',
        use: 'sig',
        kty: 'RSA'
      }
    ),
    secondary: config.oldPublicKeyFile ?
      jwtool.JWK.fromFile(
        config.oldPublicKeyFile,
        {
          algorithm: 'RS',
          use: 'sig',
          kty: 'RSA'
        }
      )
      : null
  }

  var Customs = require('../lib/customs')(log, error)

  var Server = require('../lib/server')
  var server = null
  var mailer = null
  var statsInterval = null
  var database = null
  var customs = null

  function logStatInfo() {
    log.stat(server.stat())
    log.stat(Password.stat())
  }

  require('../lib/mailer')(config, log)
    .done(
      function(m) {
        mailer = m

        var DB = require('../lib/db')(
          config,
          log,
          error,
          Token.SessionToken,
          Token.KeyFetchToken,
          Token.AccountResetToken,
          Token.<PASSWORD>,
          Token.<PASSWORD>Token
        )

        DB.connect(config[config.db.backend])
          .done(
            function (db) {
              database = db
              customs = new Customs(config.customsUrl)
              var routes = require('../lib/routes')(
                log,
                error,
                serverPublicKeys,
                signer,
                db,
                mailer,
                Password,
                config,
                customs,
                metricsContext
              )
              server = Server.create(log, error, config, routes, db)

              server.start(
                function (err) {
                  if (err) {
                    log.error({ op: 'server.start.1', msg: 'failed startup with error',
                                err: { message: err.message } })
                    process.exit(1)
                  } else {
                    log.info({ op: 'server.start.1', msg: 'running on ' + server.info.uri })
                  }
                }
              )
              statsInterval = setInterval(logStatInfo, 15000)
            },
            function (err) {
              log.error({ op: 'DB.connect', err: { message: err.message } })
              process.exit(1)
            }
          )

      }
    )

  process.on(
    'uncaughtException',
    function (err) {
      log.fatal(err)
      process.exit(8)
    }
  )
  process.on('SIGINT', shutdown)
  log.on('error', shutdown)

  function shutdown() {
    log.info({ op: 'shutdown' })
    clearInterval(statsInterval)
    server.stop(
      function () {
        customs.close()
        try {
          mailer.stop()
        } catch (e) {
          // XXX: simplesmtp module may quit early and set socket to `false`, stopping it may fail
          log.warn({ op: 'shutdown', message: 'Mailer client already disconnected' })
        }
        database.close()
        process.exit() //XXX: because of openid dep ಠ_ಠ
      }
    )
  }
}

main()

{'fraction_non_alphanumeric': 0.13123183545718756, 'fraction_numerical': 0.005141962888441762, 'mean_word_length': 1.72442143727162, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 6, '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': '3050881', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2592, 'n_words': 647}