'use strict';

var config   = require('./config');
var app      = require('./config/express');
var mongoose = require('./config/mongoose');
var passport = require('./config/passport');

// Use 'passport' authentication.
app.use(passport.initialize());

// Attach 'CORS' middleware to every route.
app.all('*', require('cors')({
	exposedHeaders: ['x-access-token']
}));

// Setup API Routes.
app.use('/api', require('./routes/auth'));
app.use('/api', require('./routes/board'));

/**
 * Error handling middleware. All errors passed to 'next' will eventually end
 * up here.
 */
app.use(function(err, req, res, next) {
	var boom = require('boom');
	    err  = boom.wrap(err, err.status);
	if(err.output.statusCode >= 500) {
		console.error(err);
	}
	return res.status(err.output.statusCode).send(err.output.payload);
});

/**
 * The Express application.
 */
module.exports.app = app;

/**
 * Perform necessary initialization to start the server.
 *
 * @param  {function}  onListen  Callback invoked when the server starts
 *                               listening to incoming requests.
 */
module.exports.listen = function(onListen) {

	var connectWithRetry = function() {
		mongoose.connect(config.mongo.url, config.mongo.opts, function (err) {
			if(err) {
				console.error(err);
				console.log("Reconnecting in " + config.mongo.timeout + " ms...");
				setTimeout(connectWithRetry, config.mongo.timeout);
			} else {
				console.log("MongoDB connected...")
			}
		});
	}

	connectWithRetry();

	this.server = app.listen(config.port, onListen || function() {
		console.log('server listening at', config.port);
	});
}

/**
 * Perform necessary teardown to stop the server.
 *
 * @param  {function=}  onShutdown  Callback invoked after shutting down.
 */
module.exports.shutdown = function(onShutdown) {
	return this.server.close(function() {
		mongoose.disconnect(onShutdown || function() {});
	});
}

@@ -1410,18 +1410,70 @@
 %09%09%09%7D
- else
+%0A%09%09%7D);%0A%09%7D%0A%0A%09mongoose.connection.on('error', function(error)
  %7B%0A
-%09%09
 %09%09co
@@ -1482,49 +1482,159 @@
 ole.
-log(%22MongoDB connected...%22)%0A%09%09%09%7D%0A%09%09%7D
+error('Error during MongoDB runtime! ' + error);%0A%09%7D);%0A%0A%09mongoose.connection.on('connected', function() %7B%0A%09%09console.error('Connected to MongoDB!'
 );%0A%09%7D
+);
 %0A%0A%09c

Add MongoDB error and reconnnect listeners