<commit_before>import _ = require('underscore');
import appConfig = require('./core/config');
import assert = require('assert');
import bodyParser = require('body-parser');
import express = require('express');
import db = require('./core/db');
import plugin = require('./core/plugin');
import util = require('util');

let app = express();

plugin.initialize();
db.initialize(appConfig.mongodb.url)
.map(() => {
  let exitHandler = () => {
    console.log('Closing db connection.');
    db.close(true);
  };
  let signalExit = (sig: string) => {
    return () => {
      console.log('Process terminated by ' + sig);
      process.exit(0);
    };
  };
  process.on('SIGINT', signalExit('SIGINT'));
  process.on('SIGTERM', signalExit('SIGTERM'));
  process.on('exit', exitHandler);
  process.on('uncaughtException', (ex: Error) => {
    console.error('Uncaught exception %j', ex);
    exitHandler();
  });

  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: false}));

  app.get('/', function (req, res) {
    res.send('Hello World!');
  });

  function handlePlugin(req: express.Request, res: express.Response) {
    plugin.get(req.params.name).handle(req, res);
  }
<commit_msg>Extract initializeExitHandler.<commit_after>import _ = require('underscore');
import appConfig = require('./core/config');
import assert = require('assert');
import bodyParser = require('body-parser');
import express = require('express');
import db = require('./core/db');
import plugin = require('./core/plugin');
import util = require('util');

let app = express();

function initializeExitHandler() {
  let exitHandler = () => {
    console.log('Closing db connection.');
    db.close(true);
  };
  let signalExit = (sig: string) => {
    return () => {
      console.log('Process terminated by ' + sig);
      process.exit(0);
    };
  };
  process.on('SIGINT', signalExit('SIGINT'));
  process.on('SIGTERM', signalExit('SIGTERM'));
  process.on('exit', exitHandler);
  process.on('uncaughtException', (ex: Error) => {
    console.error('Uncaught exception %j', ex);
    exitHandler();
  });
}

plugin.initialize();
db.initialize(appConfig.mongodb.url)
.map(() => {
  initializeExitHandler();

  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: false}));

  app.get('/', function (req, res) {
    res.send('Hello World!');
  });

  function handlePlugin(req: express.Request, res: express.Response) {
    plugin.get(req.params.name).handle(req, res);
  }

{'fraction_non_alphanumeric': 0.19435483870967743, 'fraction_numerical': 0.0008064516129032258, 'mean_word_length': 4.3585313174946005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5223998', 'n_tokens_mistral': 774, 'n_tokens_neox': 762, 'n_words': 154}