'use strict';

var
  fs         = require('fs'),
  express    = require('express'),
  mongoose   = require('mongoose'),
  debug      = require('debug')('MosaiqoApp:API:' + process.pid),
  jwt        = require('express-jwt'),
  jwtAuth    = require('src/lib/jwtAuth'),
  errors     = require('src/lib/errors'),
  routesDir  = __dirname + '/routes/';


// MONGO CONF.
// =============================================================================
var mongoConfigParser = require('src/lib/mongoConfigParser');

var mongoConn = new mongoConfigParser().setEnv({
  host     : process.env.MONGO_HOST,
  port     : process.env.MONGO_PORT,
  user     : process.env.MONGO_USER,
  password : process.env.MONGO_PASSWORD,
  database : process.env.MONGO_DATABASE
});

/* istanbul ignore next */
mongoose.connection.on('error', function () {
    debug('Mongoose connection error');
});

/* istanbul ignore next */
mongoose.connection.once('open', function callback() {
    debug('Mongoose connected to the database');
});

// connect
mongoose.connect(mongoConn.getConnectionString(), mongoConn.getConnectionOptions());


// SETUP THE MODULE ROUTES
// =============================================================================
var router = express.Router();

// auth free routes
var excluded = {path: [
  /api\/?$/i,
  /api\/auth\/?$/i
]};

// Setup the authentication using JWT
router.use( jwt({ secret: process.env.JWT_SECRET }).unless(excluded) );
router.use( jwtAuth.middleware().unless(excluded) );


// -- API ROUTES --

/**
 * @api {get} /api/ API entry point
 * @apiName Index
 * @apiGroup ROOT
 * @apiDescription Test route to make sure everything is working
 *
 * @apiExample Example usage:
 * curl -4 -i http://localhost:9000/api
 *
 * @apiSuccess {String} message Success message.
 *
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "message": "hooray! welcome to our api!"
 *     }
 */
router.get('/', function(req, res, next) {
  res.json({ message: 'hooray! welcome to our api!' });
});

// load the routes
fs.readdirSync(routesDir).forEach(function(file) {
  var route = routesDir + file.substr(0, file.indexOf('.'));
  debug('Adding route:' + route);
  require(route)(router);
});

module.exports = router;

@@ -42,16 +42,48 @@
 ('fs'),%0A
+  glob       = require('glob'),%0A
   expres
@@ -2079,33 +2079,66 @@
 tes%0A
-fs.readdirSync(routesDir)
+glob(routesDir + '**/*.js', function (er, files) %7B%0A  files
 .for
@@ -2155,16 +2155,24 @@
 ion(
-f
+routesF
 ile) %7B%0A
+  
   va
@@ -2191,15 +2191,9 @@
 utes
-Dir + f
+F
 ile.
@@ -2202,17 +2202,23 @@
 bstr(0, 
-f
+routesF
 ile.inde
@@ -2228,16 +2228,18 @@
 ('.'));%0A
+  
   debug(
@@ -2264,16 +2264,18 @@
 route);%0A
+  
   requir
@@ -2292,20 +2292,27 @@
 outer);%0A
+  
 %7D);%0A
+%7D);%0A%0A
 %0Amodule.

make routes loading recursive