var express = require( "express" );
var expressValidator = require( "express-validator" );
var jwt = require( "express-jwt" );
var logger = require( "morgan" );
var bodyParser = require( "body-parser" );
var databaseUtils = require( "api-utils" ).database;
var authUtils = require( "api-utils" ).authentication;


var routes = require( "./routes/index" );
var db = databaseUtils.mongooseConnection();

var app = express();

app.use( logger( "dev" ) );
app.use( bodyParser.json() );
app.use( expressValidator() );
app.use( bodyParser.urlencoded( {
    extended: false
} ) );

app.use( authUtils.systemAPIKey( [ "/pin-config" ] ) );

var jwtOptions = {
    secret: process.env.JWT_SECRET
};

var authNotRequired = [ "/register", "/authenticate", "/pin-config" ];
app.use( jwt( jwtOptions ).unless( { path: authNotRequired } ) );

app.use( "/", routes );

// catch 404 and forward to error handler
app.use( function ( req, res, next ) {

    var err = new Error( "Not Found" );
    err.status = 404;
    next( err );

} );

// error handlers

// development error handler
// will print stacktrace
if ( app.get( "env" ) === "development" ) {

    app.use( function ( err, req, res, next ) {

        return res.status( err.status || 500 ).json( {
            message: err.message,
            error: err
        } );

    } );

}

// production error handler
// no stacktraces leaked to user
app.use( function ( err, req, res, next ) {

    res.status( err.status || 500 ).json( {
        message: err.message,
        error: {}
    } );

} );


module.exports = app;

@@ -305,18 +305,16 @@
 cation;%0A
-%0A%0A
 var rout
@@ -343,24 +343,161 @@
 s/index%22 );%0A
+var RabbitClient = require( %22./api/utils/amqp%22 ).RabbitClient;%0A%0A%0Avar authNotRequired = %5B %22/register%22, %22/authenticate%22, %22/pin-config%22 %5D;%0A%0A
 var db = dat
@@ -529,17 +529,16 @@
 tion();%0A
-%0A
 var app 
@@ -547,24 +547,521 @@
 express();%0A%0A
+// RabbitMQ: START%0Avar rabbitURL;%0A%0Aif ( !process.env.RABBIT_1_PORT_15672_TCP && !process.env.RABBIT_URL ) %7B%0A    console.log( %22RABBIT_URL environment variable is required.%22 );%0A    process.exit( 1 );%0A%7D else %7B%0A    var rabbitURL;%0A    if ( process.env.RABBIT_URL ) %7B%0A        rabbitURL = process.env.RABBIT_URL;%0A    %7D else %7B%0A        rabbitURL = %22amqp://%22 + process.env.RABBIT_PORT_5672_TCP_ADDR;%0A    %7D%0A%7D%0A%0A%0Aconsole.log( rabbitURL );%0Aglobal.rabbitClient = new RabbitClient( rabbitURL );%0A// RabbitMQ: END%0A%0A
 app.use( log
@@ -1170,20 +1170,16 @@
 coded( %7B
-%0A   
  extende
@@ -1190,17 +1190,15 @@
 alse
-%0A
 %7D ) );%0A
-%0A
 app.
@@ -1253,31 +1253,23 @@
  );%0A
-%0Avar jwtOptions = %7B%0A   
+app.use( jwt( %7B
  sec
@@ -1299,108 +1299,10 @@
 CRET
-%0A%7D;%0A%0Avar authNotRequired = %5B %22/register%22, %22/authenticate%22, %22/pin-config%22 %5D;%0Aapp.use( jwt( jwtOptions
+ %7D
  ).u
@@ -1339,17 +1339,16 @@
  %7D ) );%0A
-%0A
 app.use(

Connect to RabbitMQ