var debug = require('debug')('server:main');
var config = require('./config.json');

var express = require('express');
var compression = require('compression');

var orm = require('./orm.js');

//init app
var app = express();
app.enable('trust proxy');
app.disable('x-powered-by');
app.use(compression({
	threshold: 0
}));

//return api uptime
app.get('/api', function (req, res) {
	res.json({
		status: 'running',
		uptime: process.uptime()
	});
});

//routes
app.use('/api/images', require('./routes/images.js'));
// app.use('/api/analyses', require('./routes/analyses.js'));
// app.use('/api/jobs', require('./routes/jobs.js'));

//static files
app.use('/images', express.static('images', {index: 'false'}));
app.use(express.static('client'));

//redirect anything else to index.html
app.use('/*', function indexRedirect(req, res, next) {
	res.sendFile('client/index.html', {root: __dirname + '/../'});
});

//error handler
app.use(function errorHandler(err, req, res, next) {
	res.status(err.status || 500).json({
		error: err.message,
		stack: err.stack
	});
});

module.exports = orm()
.then(function (models) {
	app.models = models.collections;
	app.connections = models.connections;

	app.listen(config.port, function () {
		debug('server running on port ' + config.port);
	});

	return app;
})
.catch(function (err) {
	console.log('fatal error', err);
});
@@ -159,19 +159,19 @@
 );%0A%0Avar 
-orm
+ORM
  = requi
@@ -176,19 +176,19 @@
 uire('./
-orm
+ORM
 .js');%0A%0A
@@ -1062,16 +1062,38 @@
 );%0A%7D);%0A%0A
+app.orm = new ORM();%0A%0A
 module.e
@@ -1101,19 +1101,28 @@
 ports = 
-orm
+app.orm.init
 ()%0A.then
@@ -1217,16 +1217,31 @@
 ions;%0A%0A%09
+app.listener = 
 app.list

use new orm class