Permalink
Browse files

code 'cleanup'; check for valid request; use config file

  • Loading branch information...
1 parent c46ef2d commit 74e07befb422880a8152e7e338bda0adec7777d3 @booo committed Mar 3, 2011
Showing with 84 additions and 100 deletions.
  1. +13 −5 config.js
  2. +59 −95 main.js
  3. +12 −0 package.json
View
@@ -1,5 +1,13 @@
-exports.styles = [{
- 'name' : 'testing',
- 'file' : 'mapnik.xml',
- //add some values for e.g. expire
-} ];
+exports.styles = {
+ osm : {
+ file : '../mapnik/performance_upgrade.xml',
+ "MAX_ZOOM" : 18,
+ "MIN_ZOOM" : 0,
+ expire : 60*60*24 //seconds
+ }
+}
+
+exports.port = 8000
+exports.host = 'localhost'
+
+exports.cacheFile = 'tile.tch'
View
154 main.js
@@ -1,32 +1,22 @@
-var config = require('./config'); //include config file
-var mapnik = require('mapnik');
-var TC = require('./node-tokyocabinet/build/default/tokyocabinet');
-var path = require('path');
-var clutch = require('clutch');
+var config = require('./config') //include config file
+var mapnik = require('mapnik')
+var TC = require('./node-tokyocabinet/build/default/tokyocabinet')
+var path = require('path')
+var clutch = require('clutch')
var mercator = require('mapnik/sphericalmercator')
var mappool = require('mapnik/pool')
+var http = require('http')
-//var hasher = require('crypto').createHash('md5');
-
-//var async = require('async');
-
-//var queue = async.queue(render,2);
-
-var stylesheet = path.join(__dirname,"/mapnik/osm_new.xml");
-/*var stylesheet = path.join(__dirname, "node-mapnik/examples/stylesheet.xml")*/
var maps = mappool.create(2);
console.log("Tokyo Cabinet version " + TC.VERSION);
-
var HDB = TC.HDB;
-
var hdb = new HDB;
-if (!hdb.open('tile.tch', HDB.OWRITER | HDB.OCREAT)) {
+if (!hdb.open(config.cacheFile, HDB.OWRITER | HDB.OCREAT)) {
sys.error(hdb.errmsg());
}
-
function writeResponse(content, res) {
res.writeHead(200, {
'Content-Type': 'image/png',
@@ -35,7 +25,6 @@ function writeResponse(content, res) {
'Expires:' : content.Expires,
});
res.end(new Buffer(content.data,'base64'));
-
}
var acquire = function(id,options,callback) {
@@ -55,53 +44,32 @@ var acquire = function(id,options,callback) {
delete obj;
},
idleTimeoutMillis: 20000,
-
}
maps.acquire(id, methods, function(obj) {
callback(null, obj);
});
}
-
function render(task, callback) {
- acquire(stylesheet,{}, function(err, map) {
+ acquire(task.style.file,{}, function(err, map) {
if(err) {
console.log(err);
}
else {
- console.log("map acquired\n")
-
- //map.zoom_to_box(tile2long(task.x,task.z),tile2lat(task.y,task.z),tile2long(task.x+1,task.z),tile2lat(task.y+1,task.z));
- //map.zoom_all()
- //var bbox = [tile2lat(task.y,task.z),tile2long(task.x,task.z),tile2lat(task.y-1,task.z),tile2long(task.x-1,task.z)];
- //var bbox = [13.0882097323,52.3418234221,13.7606105539,52.6697240587]
- //map.zoom_all()//map.zoom_to_box(extent);
- //map.zoom_all();
-
-
- var bbox = mercator.xyz_to_envelope(task.x, task.y, task.z, false)
- map.render(bbox,"png",function(error,data) {
+ var bbox = mercator.xyz_to_envelope(task.x, task.y, task.z, false)
+ map.render(bbox,"png",function(error,data) {
//console.log(data)
console.log("tile rendered");
if(!error) {
//TODO clean up tags, different values for different tiles
-
var temp = {
'data' : data.toString('base64'),
'timestamp' : new Date().getTime(),
'ETag' : require('crypto').createHash('md5').update(data).digest('hex'),
- 'Expires' : new Date(new Date().getTime()+3600000).toGMTString(),
+ 'Expires' : new Date(new Date().getTime()+task.style.expire).toGMTString(),
};
-
writeResponse(temp,task.res)
- console.log(temp.data)
- //console.log(temp.timestamp);
- //console.log(temp.ETag);
- //TODO do async
- //hdb.put(task.url, JSON.stringify(temp));
- //hdb.put(url, data.toString('base64'));
-
hdb.putAsync(task.url, JSON.stringify(temp), function(err) {
if(err) {
console.log(err);
@@ -123,55 +91,56 @@ function render(task, callback) {
}
function requestHandler(req, res, style, z, x, y) {
- //console.log(style, z, x, y);
var url = style + "/" + z + "/" + x + "/" + y;
-
- hdb.getAsync(url, function(err, value) {
- //console.log(value);
- if(err) {
- var renderTask = {
- 'url' : url,
- 'res' : res,
- 'stylesheet' : style,
- 'z' : parseInt(z),
- 'x' : parseInt(x),
- 'y' : parseInt(y)
- };
-
- //queue.push(renderTask, function() {});
- //var data = map.render_to_string();
- render(renderTask,function() {});
- }
- else {
- //console.log(value);
- var content = JSON.parse(value);
- //TODO changeable expire time... 1000*60*60*24 = one day
- if((new Date().getTime() - content.timestamp) > 1000*60*60) {
- //rerender
- var renderTask = {
- 'url' : url,
- 'res' : res,
- 'stylesheet' : style,
- 'z' : parseInt(z),
- 'x' : parseInt(x),
- 'y' : parseInt(y)
- };
- //response is send in render function
- /*queue.push(renderTask, function() {});*/
+ console.log(config.styles[style])
+ if(config.styles[style] != undefined
+ && z >= config.styles[style].MIN_ZOOM && z <= config.styles[style].MAX_ZOOM
+ && x >= 0 && x < Math.pow(2,z)
+ && y >= 0 && y < Math.pow(2,z)) {
+
+ hdb.getAsync(url, function(err, value) {
+ if(err) {
+ var renderTask = {
+ 'url' : url,
+ 'res' : res,
+ 'style' : config.styles[style],
+ 'z' : parseInt(z),
+ 'x' : parseInt(x),
+ 'y' : parseInt(y)
+ };
render(renderTask,function() {});
- }
- else {
- //tile is not expired
- writeResponse(content, res);
- }
- }
- });
-
-
+ }
+ else {
+ var content = JSON.parse(value);
+ //TODO changeable expire time... 1000*60*60*24 = one day
+ if((new Date().getTime() - content.timestamp) > config.styles[style].expire) {
+ //rerender
+ var renderTask = {
+ 'url' : url,
+ 'res' : res,
+ 'style' : config.styles[style],
+ 'z' : parseInt(z),
+ 'x' : parseInt(x),
+ 'y' : parseInt(y)
+ };
+ //response is send in render function
+ render(renderTask,function() {});
+ }
+ else {
+ //tile is not expired
+ writeResponse(content, res);
+ }
+ }
+ });
+
+ } else {
+ console.log("bad request")
+ res.writeHead(404,{})
+ res.end()
+ }
}
-
-var myRoutes = clutch.route404([
+var routes = clutch.route404([
['GET /(\\w+)/(\\d+)/(\\d+)/(\\d+).png$', requestHandler],
/*['HEAD /(\\w+)/(\\d+)/(\\d+)/(\\d+).png$', requestHandler],
['GET /(\\w+)/(\\d+)/(\\d+)/(\\d+).png/dirty$', requestHandler],
@@ -180,10 +149,5 @@ var myRoutes = clutch.route404([
['HEAD /(\\w+)/(\\d+)/(\\d+)/(\\d+).png/status$', requestHandler],*/
]);
-
-
-
-var http = require('http');
-http.createServer(myRoutes).listen(8000, 'localhost');
-
-
+http.createServer(routes).listen(config.port, config.host)
+console.log('Server started...')
View
@@ -0,0 +1,12 @@
+{
+ name : "node_tile",
+ version : "0.0.23",
+ description : "simple tile server with integrated cache",
+ autor : "booo",
+ files : ["."],
+ dependencies : {
+ clutch : "*",
+ mapnik : "*",
+ //"node-tokyocabinet" : "*"
+ }
+}

0 comments on commit 74e07be

Please sign in to comment.