{"size": 1565, "ext": "js", "max_stars_repo_path": "lib/index.js", "max_stars_repo_name": "Woellchen/node-imagediff", "max_stars_repo_stars_event_min_datetime": "2015-01-27T05:58:49.000Z", "max_stars_repo_stars_event_max_datetime": "2018-12-03T12:34:35.000Z", "max_issues_repo_path": "lib/index.js", "max_issues_repo_name": "Woellchen/node-imagediff", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/index.js", "max_forks_repo_name": "Woellchen/node-imagediff", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.5842696629, "max_line_length": 103, "alphanum_fraction": 0.6095846645}
var Hapi = require('hapi'),
	swig = require('swig'),
	compare = require('./imagemagick').compare;

var isProduction = process.env.NODE_ENV === 'production';
var host = process.env.HOST || '0.0.0.0';
var port = process.env.PORT || 80;

var options = {
	views: {
		path: 'templates',
		engines: {
			html: {
				module: swig
			}
		}
	}
};

if (!isProduction) {
	swig.setDefaults({
		cache: false
	});

	options.views.isCached = false;
	options.debug = {
		request: ['error']
	};
	options.json = {
		space: 4
	};
}

var server = Hapi.createServer(host, port, options);

server.route({
	'path': '/{path*}',
	'method': 'GET',
	'handler': {
		'directory': {
			'path': 'static',
			'index': false
		}
	}
});
server.route({
	method: 'GET',
	path: '/',
	handler: function(request, reply) {
		reply.view('index.html', {
			title: 'hello world'
		});
	}
});
server.route({
	method: 'POST',
	path: '/compare',
	handler: function(request, reply) {
		if (typeof request.payload.image1 === 'undefined' || typeof request.payload.image2 === 'undefined') {
			reply(Hapi.error.badRequest('Compare failed!'));

			return;
		}

		compare(request.payload.image1.path, request.payload.image2.path, function(err, data) {
			if (err) {
				reply(Hapi.error.badRequest('Compare failed!', err));

				return;
			}

			reply(data).type('image/png');
		});
	},
	config: {
		payload: {
			output: 'file'
		}
	}
});

server.on('error', function () {
	console.log(arguments);
});

server.start();

console.log('server started listening on http://' + server.info.host + ':' + server.info.port);
