Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created March 30, 2015 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganherlocker/b14e17752227b0647e8c to your computer and use it in GitHub Desktop.
Save morganherlocker/b14e17752227b0647e8c to your computer and use it in GitHub Desktop.
fs = require('fs');
http = require('http');
url = require('url');
exec = require('child_process').execSync
http.createServer(function(req, res){
var request = url.parse(req.url, true);
if (request.pathname.split('/')[1] === 'tile') {
var tile = request.pathname.split('/')[2].split('.')[0].split('-')
res.writeHead(200, {'Content-Type': 'image/png' });
var img
if(fs.existsSync(__dirname+'/tile/'+tile[0]+'-'+tile[1]+'-'+tile[2]+'.png')){
img = fs.readFileSync(__dirname+'/tile/'+tile[0]+'-'+tile[1]+'-'+tile[2]+'.png', 'binary')
} else {
console.log('write: '+tile[0]+'/'+tile[1]+'/'+tile[2])
img = exec('geotype ./dc.json -p -f 0 -m 2 -t '+tile[0]+'/'+tile[1]+'/'+tile[2])
fs.writeFileSync(__dirname+'/tile/'+tile[0]+'-'+tile[1]+'-'+tile[2]+'.png',img)
}
res.end(img, 'binary');
} else {
res.writeHeader(200, {"Content-Type": "text/html"});
res.write(fs.readFileSync(__dirname+'/index.html'));
res.end();
}
}).listen(8080, '127.0.0.1');
console.log('listening 127.0.0.1:8080');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment