var http = require('http');
var phantom = require('node-phantom-simple');

var processing = false;
var requests = [];

// Request the current page and send back as png
// Then process next request in queue if there is one
var processRequest = function(req, res){
	if(req && res){
		processing = true;
		phantom.create(function(err,ph) {
			return ph.createPage(function(err, page) {
				page.set('viewportSize', {
					width: 1024,
					height: 1024
				});
				return page.open(req.url.slice(1), function(err, status) {
					console.log('status: ', status);
					//Wait for a bit for AJAX content to load on the page. Better solution?
					setTimeout(function() {
						page.renderBase64("PNG", function(error, result){
							var imageBuffer = new Buffer(result, 'base64');
							res.writeHead(200, {
								'Content-Type': 'image/png',
								'Content-Length': imageBuffer.length});
							res.end(imageBuffer);
							ph.exit()
							processRequest.apply(null, requests.shift());
						});
					}, 1000);
				});
			}, {parameters: {'ignore-ssl-errors': 'yes'}});
		});
	} else {
		processing = false;
	}
};

// Process the incoming request if not already processing
// Else put into queue
http.createServer(function (req, res) {
	if(!processing){
		processRequest(req, res);
	} else if (requests.length > 10) {
		requests = [];
		processRequest(req, res);
	} else {
		requests.push([req, res]);
	}
}).listen(process.env.PORT || 1337, function(){
	console.log('running');
});

var http = require('http');
var phantom = require('node-phantom-simple');

var processing = false;
var requests = [];

// Request the current page and send back as png
// Then process next request in queue if there is one
var processRequest = function(req, res){
	if(req && res){
		processing = true;
		phantom.create(function(err,ph) {
			return ph.createPage(function(err, page) {
				page.set('viewportSize', {
					width: 1024,
					height: 1024
				});
				return page.open(req.url.slice(1), function(err, status) {
					console.log('status: ', status, err);
					//Wait for a bit for AJAX content to load on the page. Better solution?
					setTimeout(function() {
						page.renderBase64("PNG", function(error, result){
							var imageBuffer = new Buffer(result, 'base64');
							res.writeHead(200, {
								'Content-Type': 'image/png',
								'Content-Length': imageBuffer.length});
							res.end(imageBuffer);
							ph.exit()
							processRequest.apply(null, requests.shift());
						});
					}, 1000);
				});
			}, {parameters: {'ignore-ssl-errors': 'yes'}});
		});
	} else {
		processing = false;
	}
};

// Process the incoming request if not already processing
// Else put into queue
http.createServer(function (req, res) {
	if(!processing){
		processRequest(req, res);
	} else if (requests.length > 10) {
		requests = [];
		processRequest(req, res);
	} else {
		requests.push([req, res]);
	}
}).listen(process.env.PORT || 1337, function(){
	console.log('running');
});

print the page loading error if any