'use strict';
/**
 * Module dependencies.
 */
var init = require('./config/init')(),
    mongoose = require('mongoose'),
    config = require('./config/config'),
    chalk = require('chalk');

// Avoids DEPTH_ZERO_SELF_SIGNED_CERT error for self-signed certs
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

/**
 * Main application entry file.
 * Please note that the order of loading is important.
 */

global.__config_path = config.root + '/config';
global.__libs_path = config.root + '/app/libs';

// Init the express application
var app = require('./config/express')();

app.on('error', function(err) {
    if (err.errno === 'EADDRINUSE') {
        console.log('Port already in use.');
        process.exit(1);
    } else {
        console.log(err);
    }
});

// Start the app by listening on <port>
app.listen(config.port);

var mongoDB = "mongodb://localhost:27017/db_hoadonviet";
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.Promise = global.Promise;
var db = mongoose.connection;

db.on("error", console.error.bind(console, "MongoDB connection error:"));

// Expose app
module.exports = app;
