Skip to content

Commit

Permalink
Separate compression and logging
Browse files Browse the repository at this point in the history
Rely on user to specify express modules and customizability they want.
Expanded example to show case before and after hooks and also possible
express plugins
  • Loading branch information
BHare1985 committed Jun 12, 2016
1 parent 2f47fcf commit d142c3a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#Windwalker 0.0.3
#Windwalker


Install Node 0.12:
http://nodejs.org/dist/v0.12.0/x64/node-v0.12.0-x64.msi

This is derived from a very early version of Windshaft.

It uses modified MapBox modules [grainstore] and [millstone] to allow it to run under Windows or Linux.


[grainstore]: <http://github.com/BHare1985/grainstore>
[millstone]: <http://github.com/BHare1985/millstone>

This is derived from a very early version of [Windshaft](https://github.com/CartoDB/Windshaft) which is probably better if you can run under Linux and PostGIS. The purpose of this module is that it uses modified MapBox modules [grainstore](http://github.com/BHare1985/grainstore) and [millstone](http://github.com/BHare1985/millstone) to allow it to run under Windows or Linux.
85 changes: 80 additions & 5 deletions examples/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

var Windshaft = require('../lib');
var _ = require('underscore');

var config = {
dbtype: 'mssql',
geom_type: 'polygon',
tileRoute: '/:dbname/tiles/:table/:z/:x/:y.*',
grainstore: {
map: {srid: 3857},
datasource: {
Expand All @@ -25,6 +25,7 @@ var config = {
polygon: "::line {line-color: red; line-width: 4; line-join: round; line-cap: round;}",
},
},
showErrors: true,
enableCors: true,
parameterParser: function(req, callback){

Expand All @@ -37,8 +38,82 @@ var config = {
}
};

// Initialize tile server on port 4000
var ws = new Windshaft.Server(config);
ws.listen(4000);
// Initialize with configuration and get back express4 server object
var app = new Windshaft.Server(config);

//Enable compression for grid tiles (requires module)
//app.use(compression()) // Will not compress png by default, see module

// Use morgan logger with "dev" log type (requires module)
//app.use(morgan("dev"));


// Setup fakemaps
app.setupMap('fakemap');
app.setupMap('fakemap2', { dbtype: 'postgres' }); // Use postgres instead of mssql

//Setup custom tile route for fakemap,
app.get('/fakemaps/:z/:x/:y.*', function (req, res) {
app.processTileRoute('fakemap', req, res);
app.processTileRoute('fakemap2', req, res, app.parameterParser);
});


//Setup a map with key "map1" with overriding configuration
// This is the map we will use in the viewer
app.setupMap('map1', {
grainstore: {
properties: {
'cache-features': 'on'
}
}
});

//Setup custom tile route, alternatively use config.tileRoute
app.get('/:dbname/tiles/:table/:z/:x/:y.*', function (req, res) {
app.processTileRoute('map1', req, res, app.parameterParser, app.beforeTileRender, app.afterTileRender);
});



app.parameterParser = function(req, callback){
// This is where pre-processing comes in place. I set the interactivity
// field to be name so it popups on the map
req.params.interactivity = 'name';

// Convert every query variable to a request parameter (sql, style, table, etc)
_.extend(req.params, req.query);

// send the finished req object on
callback(null,req);
};


app.beforeTileRender = function(req, res, callback) {
// Great place to check if tile has been modified and rather or not to send
// a 304 and skip a database call. Just set tile to non-null or the tile body
// and getting the tile will be skipped

//Setup fake header to showcase beforeTileRender() function
res.setHeader('x-beforeTileRender', new Date());

var tile = null;
var headers = null;
callback(null, tile, headers);
};


app.afterTileRender = function(req, res, tile, headers, callback) {
// Great place to save processed tiles to a cache and to be checked in beforeTileRender
// You have access to the tile and current headers for logic/processing

//Setup fake header to showcase beforeTileRender() function
headers['x-afterTileRender'] = new Date();
return callback(null, tile, headers);
};


console.log("map tiles are now being served out of: http://localhost:4000" + config.tileRoute);
// Have the server listen on port 4000
var listener = app.listen(4000, function(){
console.log("map tiles are now being served out of: http://localhost:" + listener.address().port + config.tileRoute);
});
17 changes: 0 additions & 17 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ var RenderCache = require('./render_cache');
var _ = require('underscore');
var lodash = require('lodash');
var Step = require('step');
var compression = require('compression');
var morgan = require('morgan')

function shouldCompress(req, res) {
if (req.headers['Content-Type'] !== 'application/json') {
return false;
}

return compression.filter(req, res)
}

module.exports = function(opts){
var opts = opts || {};

Expand All @@ -34,14 +24,7 @@ module.exports = function(opts){

// initialize express server
var app = express();

app.use(compression({filter: shouldCompress}))

if(opts.logRequests) {
var logType = (typeof opts.logRequests === "string") ? opts.logRequests : "dev";
app.use(morgan(logType))
}

// extend app with all opts propeties and methods
_.extend(app, opts);

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
}
],
"dependencies": {
"compression": "1.5.2",
"express": "4.13.4",
"generic-pool": "1.0.12",
"grainstore": "https://github.com/BHare1985/grainstore/tarball/1.1.0-hare2",
"lodash": "4.4.0",
"mapnik": "3.4.5",
"morgan": "1.7.0",
"qs": "5.2.0",
"step": "0.0.6",
"tilelive": "5.8.2",
Expand Down

0 comments on commit d142c3a

Please sign in to comment.