<commit_before>var express = require('express');
var bodyParser = require('body-parser');
var passport = require('passport');
var http = require('http');

var configPath = './config/app.js';
process.argv.forEach((val, index) => {
  if (index > 1) {
    if (val.toLowerCase().startsWith('--config=')) {
      configPath = val.split('=')[1];
    }
    else {
      var error = "Cannot parse '" + val + "'.  The only accepted parameter is --config=<path-to-config-file> ";
      throw error;
    }
  }
});

var config = require('./config');
config.loadConfig(configPath);

var modelsUtil = require('./models/util/util');
var log = require('./logging');
var models = require('./models');

log.info('Starting Full Noise.');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());
log.addExpressApp(app);

// Adding API documentation
app.use(express.static(__dirname + '/apidoc'));

// Adding headers to allow cross-origin HTTP request.
// This is so the web interface running on a different port/domain can access the API.
// This could cause security issues with Cookies but JWTs are used instead of Cookies.
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', req.headers.origin);
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS, PATCH');
  res.header('Access-Control-Allow-Headers', 'where, offset, limit, Authorization');
  next();
});

require('./api/V1')(app);
app.use(require('./api/customErrors').errorHandler);

// Add file processing API.
var fileProcessingApp = express();
fileProcessingApp.use(bodyParser.urlencoded({ extended: false }));
require('./api/fileProcessing')(fileProcessingApp);
http.createServer(fileProcessingApp).listen(config.fileProcessing.port);
log.info('Starting file processing on', config.fileProcessing.port);

log.info("Connecting to database.....");
models.sequelize
  .authenticate()
  .then(() => log.info("Connected to database."))
  .then(() => checkS3Connection())
  .then(() => openHttpServer(app))
  .catch(function(error) {
    log.error(error);
  });

function openHttpServer(app) {
  return new Promise(function(resolve, reject) {
    if (!config.server.http.active)
    {return resolve();}
    try {
      log.info('Starting http server on ', config.server.http.port);
      http.createServer(app).listen(config.server.http.port);
      return resolve();
    } catch (err) {
      return reject(err);
    }
  });
}

// Returns a Promise that will reolve if it could connect to the S3 file storage
// and reject if connection failed.
function checkS3Connection() {
  return new Promise(function(resolve, reject) {
    var s3 = modelsUtil.openS3();
    var params = { Bucket: config.s3.bucket };
    log.info("Connecting to S3.....");
    s3.headBucket(params, function(err) {<commit_msg>Exit if PostgreSQL or Minio connection fails<commit_after>var express = require('express');
var bodyParser = require('body-parser');
var passport = require('passport');
var process = require('process');
var http = require('http');

var configPath = './config/app.js';
process.argv.forEach((val, index) => {
  if (index > 1) {
    if (val.toLowerCase().startsWith('--config=')) {
      configPath = val.split('=')[1];
    }
    else {
      var error = "Cannot parse '" + val + "'.  The only accepted parameter is --config=<path-to-config-file> ";
      throw error;
    }
  }
});

var config = require('./config');
config.loadConfig(configPath);

var modelsUtil = require('./models/util/util');
var log = require('./logging');
var models = require('./models');

log.info('Starting Full Noise.');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());
log.addExpressApp(app);

// Adding API documentation
app.use(express.static(__dirname + '/apidoc'));

// Adding headers to allow cross-origin HTTP request.
// This is so the web interface running on a different port/domain can access the API.
// This could cause security issues with Cookies but JWTs are used instead of Cookies.
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', req.headers.origin);
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS, PATCH');
  res.header('Access-Control-Allow-Headers', 'where, offset, limit, Authorization');
  next();
});

require('./api/V1')(app);
app.use(require('./api/customErrors').errorHandler);

// Add file processing API.
var fileProcessingApp = express();
fileProcessingApp.use(bodyParser.urlencoded({ extended: false }));
require('./api/fileProcessing')(fileProcessingApp);
http.createServer(fileProcessingApp).listen(config.fileProcessing.port);
log.info('Starting file processing on', config.fileProcessing.port);

log.info("Connecting to database.....");
models.sequelize
  .authenticate()
  .then(() => log.info("Connected to database."))
  .then(() => checkS3Connection())
  .then(() => openHttpServer(app))
  .catch(function(error) {
    log.error(error);
    process.exit(2);
  });

function openHttpServer(app) {
  return new Promise(function(resolve, reject) {
    if (!config.server.http.active)
    {return resolve();}
    try {
      log.info('Starting http server on ', config.server.http.port);
      http.createServer(app).listen(config.server.http.port);
      return resolve();
    } catch (err) {
      return reject(err);
    }
  });
}

// Returns a Promise that will reolve if it could connect to the S3 file storage
// and reject if connection failed.
function checkS3Connection() {
  return new Promise(function(resolve, reject) {
    var s3 = modelsUtil.openS3();
    var params = { Bucket: config.s3.bucket };
    log.info("Connecting to S3.....");
    s3.headBucket(params, function(err) {
{'fraction_non_alphanumeric': 0.16792582417582416, 'fraction_numerical': 0.003949175824175824, 'mean_word_length': 4.1962533452274755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1906615', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1737, 'n_words': 448}