const express = require('express'),
	app = express(),
	bodyParser = require('body-parser'),
	morgan = require('morgan'),
	toml = require('toml'),
	fs = require('fs'),
	settings = toml.parse(fs.readFileSync('./settings.toml')),
	Database = require('./structures/Database'),
	mailTransport = require('./structures/mailTransport'),
	db = new Database(settings.mongo),
	raven = require('raven');

mailTransport.config({ from: settings.email.from });

if (process.env.NODE_ENV === 'production') {
	raven.disableConsoleAlerts();
	raven.config(settings.raven.url, {
		release: (require('./package.json')).version,
		autoBreadcrumbs: { 'http': true },
		captureUnhandledRejections: true
	}).install();
}

app.set('trust proxy', 'loopback');
app.set('env', 'production');
app.disable('x-powered-by');

app.locals.jwt_signingkey = fs.readFileSync(__dirname + '/jwt_signingkey.key');

app.use(raven.requestHandler());
app.use(raven.errorHandler());
app.use(morgan(':req[cf-connecting-ip] :method :url :status :response-time[0]ms', {
	skip: (req, res) => res.statusCode < 400 // Only log failed requests/responses
}));
// Parse data into req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// In dev we need a way to get images
if (process.env.NODE_ENV === 'development') {
	app.use('/image', express.static('image', { index: false, extensions: ['jpg'] }));
	app.use((req, res, next) => {
		res.set('Access-Control-Allow-Origin', '*');
		res.set('Access-Control-Allow-Credentials', 'true');
		res.set('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT');
		res.set('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization');
		return next();
	});
}

// Load in the routes for express
let apiv1 = new (require('./api/v1/Router.js'))(settings, db, mailTransport);
app.use(apiv1.path, apiv1.router);

// Start the express server
app.listen(settings.port, error => {
	if (error)
		return console.log(error)
	console.log('Server online');
});

@@ -1290,19 +1290,17 @@
 opment')
- %7B
 %0A
+
 %09app.use
@@ -1375,17 +1375,17 @@
 '%5D %7D));%0A
-%09
+%0A
 app.use(
@@ -1406,17 +1406,16 @@
 t) =%3E %7B%0A
-%09
 %09res.set
@@ -1448,25 +1448,24 @@
 gin', '*');%0A
-%09
 %09res.set('Ac
@@ -1502,25 +1502,24 @@
 ', 'true');%0A
-%09
 %09res.set('Ac
@@ -1556,13 +1556,8 @@
 GET,
-HEAD,
 OPTI
@@ -1572,17 +1572,16 @@
 ,PUT');%0A
-%09
 %09res.set
@@ -1651,16 +1651,17 @@
  Origin,
+ 
 Accept, 
@@ -1772,17 +1772,16 @@
 tion');%0A
-%09
 %09return 
@@ -1792,14 +1792,11 @@
 ();%0A
-%09
 %7D);
-%0A%7D
 %0A%0A//

Fix headers