
/*!
 * nodejs-express-mongoose-demo
 * Copyright(c) 2013 Madhusudhan Srinivasa <madhums8@gmail.com>
 * MIT Licensed
 */
/**
 * Module dependencies
 */

global.rekuire = require('rekuire')
var fs = require('fs');
var express = require('express');
var mongoose = require('mongoose');
var config = rekuire('config');
var async = require('async');
var app = express();
var port = process.env.PORT || 3000;

var elderwand = require('elderwand');
elderwand.environment();
// Connect to mongodb
var connect = function () {
  var options = { server: { socketOptions: { keepAlive: 1 } } };
  mongoose.connect(config.db, options);
};
connect();

mongoose.connection.on('error', console.log);
mongoose.connection.on('disconnected', connect);

async.series([
    function(next) { 
    }
 ]);
// Bootstrap models
fs.readdirSync(__dirname + '/app/models').forEach(function (file) {
	console.log(__dirname + '/app/models/' + file)
  if (~file.indexOf('.js')) require(__dirname + '/app/models/' + file);
});
// Bootstrap controllers
fs.readdirSync(__dirname + '/app/controllers').forEach(function (file) {
  if (~file.indexOf('.js')) require(__dirname + '/app/controllers/' + file);
});
var passport = require('passport');

// Bootstrap passport config
require('./config/passport')(passport, config);

// Bootstrap application settings
require('./config/express')(app, passport);

// Bootstrap routes
require('./config/routes')(app, passport);

app.listen(port);
console.log('Express app started on port ' + port);

/**
 * Expose
 */ 

module.exports = app;
