#!/usr/bin/env node
var debug = require('debug')('explorer');
var settings = require('../lib/settings');
var db = require('../lib/database');
var app = require('../app');

app.set('port', process.env.PORT || settings.webserver.port);

var dbString = 'mongodb://' + encodeURIComponent(settings.dbsettings.user);
dbString = dbString + ':' + encodeURIComponent(settings.dbsettings.password);
dbString = dbString + '@' + settings.dbsettings.address;
dbString = dbString + ':' + settings.dbsettings.port;
dbString = dbString + '/' + settings.dbsettings.database;

db.connect(dbString, function() {
  // initialize the database
  db.initialize_data_startup(function() {
    var server = app.listen(app.get('port'), '::', function() {
      debug('Express server listening on port ' + server.address().port);
    });

    process.on('SIGINT', () => {
      server.close(() => {
        var mongoose = require('mongoose');

        mongoose.connection.close(false, () => {
          // close the main process now that all http and database connections have closed
          process.exit(0);
        });
      });
    });
  });
});