From f802a0c79b3a562017aa4b6e75ff568ca72daca5 Mon Sep 17 00:00:00 2001 From: Rodolfo Wilhelmy Date: Fri, 6 May 2016 13:57:31 -0700 Subject: [PATCH 1/6] Separate cli and server code --- README.md | 2 +- cli.js | 32 ++++++++++++++++++++++++++ mbview.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ views/map.ejs | 10 ++++---- 4 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 cli.js create mode 100644 mbview.js diff --git a/README.md b/README.md index a0bbce7..e449605 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,5 @@ Watch MBTiles in your localhost ```bash % npm install % open http://localhost:9000 -% node server.js --mbtiles ~/foo.mbtiles --port 9000 +% node cli.js --mbtiles ~/foo.mbtiles --port 9000 ``` diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..3943a0d --- /dev/null +++ b/cli.js @@ -0,0 +1,32 @@ +var argv = require('minimist')(process.argv.slice(2)); +var path = require('path'); + +if (!argv.mbtiles) { + console.log(usage()); + process.exit(1); +} + +function usage () { + var text = []; + text.push('usage: node cli.js [options]'); + text.push(''); + text.push(' --mbtiles path to mbtiles file'); + text.push(' --port sets port to use'); + text.push(' --help prints this message'); + text.push(''); + return text.join('\n'); +} + +var MBView = require('./mbview'); +var params = { + center: argv.center || [-122.42, 37.75], + mbtiles: argv.mbtiles, + port: argv.port || 3000, + sourceLayer: argv.sourceLayer || path.basename(argv.mbtiles, '.mbtiles'), + sourceId: 'default', + zoom: 12 +}; + +MBView.serve(params, function (err, config) { + console.log('Listening on http://localhost:' + config.port); +}); diff --git a/mbview.js b/mbview.js new file mode 100644 index 0000000..bd3fbe0 --- /dev/null +++ b/mbview.js @@ -0,0 +1,63 @@ +var express = require('express'); +var app = express(); +var MBTiles = require('mbtiles'); + +app.set('view engine', 'ejs'); +app.use(express.static('public')); + +module.exports = { + + /** + * Create a MBTiles interface, grab some metadata and spin up server. + * @param {Object} basic configuration, e.g. port + * @param {Function} a callback with the server configuration loaded + */ + serve: function (config, callback) { + console.log('*** Reading from', config.mbtiles); + var listen = this.listen; + + new MBTiles(config.mbtiles, function(err, tiles) { + if (err) throw err; + tiles.getInfo((err, data) => { + if (err) throw err; + console.log('*** Metadata found in the MBTiles'); + console.log(data); + + config.zoom = data.center.pop(); + config.center = data.center; + config.sourceId = data.id; + config.sourceLayer = data.vector_layers[0].id; + + listen(config, tiles, callback); + }); + }); + }, + + listen: function (config, tiles, onListen) { + app.get('/', (req, res) => { + res.render('map', config); + }); + + app.get('/debug/:z/:x/:y.pbf', (req, res) => { + var p = req.params; + console.log('Serving', p.z + '/' + p.x + '/' + p.y); + tiles.getTile(p.z, p.x, p.y, (err, tile, headers) => { + if (err) { + console.error(err); + res.end(); + } else { + console.log(headers); + res.setHeader('Content-Type', headers['Content-Type']); + res.setHeader('Content-Encoding', headers['Content-Encoding']); + res.setHeader('Last-Modified', headers['Last-Modified']); + res.send(tile); + } + }); + }); + + app.listen(config.port, function () { + onListen(null, config); + }); + } + +} diff --git a/views/map.ejs b/views/map.ejs index c4dff99..07bd4b5 100644 --- a/views/map.ejs +++ b/views/map.ejs @@ -19,23 +19,23 @@ mapboxgl.accessToken = 'pk.eyJ1Ijoicm9kb3dpIiwiYSI6ImdZdDkyQU0ifQ.bPu86kwHgaenPh var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/dark-v8', - center: [<%= config.center %>], - zoom: <%= config.zoom %> + center: [<%= center %>], + zoom: <%= zoom %> }); map.on('load', function () { map.addSource('debug', { type: 'vector', tiles: [ - 'http://localhost:<%= config.port %>/debug/{z}/{x}/{y}.pbf' + 'http://localhost:<%= port %>/debug/{z}/{x}/{y}.pbf' ], maxzoom: 12 }); map.addLayer({ - 'id': '<%= config.sourceId %>', + 'id': '<%= sourceId %>', 'type': 'line', 'source': 'debug', - 'source-layer': '<%= config.sourceLayer %>', + 'source-layer': '<%= sourceLayer %>', 'layout': { 'line-join': 'round', 'line-cap': 'round' From a8597f50a0892c8b433708b507a165a7245a4a6a Mon Sep 17 00:00:00 2001 From: Rodolfo Wilhelmy Date: Fri, 6 May 2016 14:03:15 -0700 Subject: [PATCH 2/6] Set response headers as they come --- mbview.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mbview.js b/mbview.js index bd3fbe0..03d5390 100644 --- a/mbview.js +++ b/mbview.js @@ -47,10 +47,8 @@ module.exports = { res.end(); } else { console.log(headers); - res.setHeader('Content-Type', headers['Content-Type']); - res.setHeader('Content-Encoding', headers['Content-Encoding']); - res.setHeader('Last-Modified', headers['Last-Modified']); - res.send(tile); + res.writeHead(200, headers); + res.end(tile); } }); }); From 1a5c59a121a5001503f91423c530d6bb54851e32 Mon Sep 17 00:00:00 2001 From: Rodolfo Wilhelmy Date: Fri, 6 May 2016 14:05:29 -0700 Subject: [PATCH 3/6] Set bin script for global install --- cli.js | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 3943a0d..4867404 100644 --- a/cli.js +++ b/cli.js @@ -1,3 +1,5 @@ +#!/usr/bin/env node + var argv = require('minimist')(process.argv.slice(2)); var path = require('path'); diff --git a/package.json b/package.json index d434c69..6881b42 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "Watch mbtiles in your localhost", "bin": { - "mbview": "./server.js" + "mbview": "./cli.js" }, "main": "server.js", "scripts": { From 38d619ff378c5a32bc708897720fcf79d9d6ed4a Mon Sep 17 00:00:00 2001 From: Rodolfo Wilhelmy Date: Fri, 6 May 2016 14:09:01 -0700 Subject: [PATCH 4/6] Update README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index e449605..8a1ceeb 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,10 @@ Watch MBTiles in your localhost % open http://localhost:9000 % node cli.js --mbtiles ~/foo.mbtiles --port 9000 ``` + +Global install + +```bash +% npm install -g mbview +% mbview +``` From 6978bdd4bd719c5fe33c24719519635b8ad033d9 Mon Sep 17 00:00:00 2001 From: Rodolfo Wilhelmy Date: Fri, 6 May 2016 14:15:03 -0700 Subject: [PATCH 5/6] Replacing server.js for cli/mbview js --- server.js | 80 ------------------------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 server.js diff --git a/server.js b/server.js deleted file mode 100644 index 659854b..0000000 --- a/server.js +++ /dev/null @@ -1,80 +0,0 @@ -var argv = require('minimist')(process.argv.slice(2)); - -if (!argv.mbtiles) { - console.log(usage()); - process.exit(1); -} - -var express = require('express'); -var app = express(); -var MBTiles = require('mbtiles'); -var path = require('path'); -var tiles = null; -var config = { - center: [-122.42709159851074, 37.75987547727969], - port: argv.port || 3000, - sourceLayer: path.basename(argv.mbtiles, '.mbtiles'), - sourceId: 'default', - zoom: 12 -}; - -function usage () { - var text = []; - text.push('usage: node server.js [options]'); - text.push(''); - text.push(' --mbtiles path to mbtiles file'); - text.push(' --port sets port to use'); - text.push(' --help prints this message'); - text.push(''); - return text.join('\n'); -} - -function loadTiles(path) { - console.log('*** Reading from', path); - - const mb = new MBTiles(path, function(err, mbtiles) { - if (err) throw err; - tiles = mbtiles; - mbtiles.getInfo(function(err, data) { - if (err) throw err; - console.log('*** Metadata found in the MBTiles'); - console.log(data); - // MBTiles returns center as [lon, lat, zoom] - config.zoom = data.center.pop(); - config.center = data.center; - // SEE server input in console for more info on these params - config.sourceId = data.id; - config.sourceLayer = data.vector_layers[0].id; - }); - }); - -} - -app.get('/', function(req, res) { - res.render('map', { config: config }); -}); - -app.get('/debug/:z/:x/:y.pbf', function(req, res) { - var p = req.params; - console.log('Serving', p.z + '/' + p.x + '/' + p.y); - tiles.getTile(p.z, p.x, p.y, function(err, tile, headers) { - if (err) { - console.error(err); - res.end(); - } else { - console.log(headers); - res.setHeader('Content-Type', headers['Content-Type']); - res.setHeader('Content-Encoding', headers['Content-Encoding']); - res.setHeader('Last-Modified', headers['Last-Modified']); - res.send(tile); - } - }); -}); - -app.use(express.static('public')); -app.set('view engine', 'ejs'); - -app.listen(config.port, function () { - console.log('Listening on http://localhost:' + config.port); - loadTiles(argv.mbtiles); -}); From 6ab42b28a8626cde0e6d284c878f052c94688478 Mon Sep 17 00:00:00 2001 From: Rodolfo Wilhelmy Date: Fri, 6 May 2016 14:17:07 -0700 Subject: [PATCH 6/6] Remove ES5 arrow funcs --- mbview.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mbview.js b/mbview.js index 03d5390..2ba04cb 100644 --- a/mbview.js +++ b/mbview.js @@ -18,7 +18,7 @@ module.exports = { new MBTiles(config.mbtiles, function(err, tiles) { if (err) throw err; - tiles.getInfo((err, data) => { + tiles.getInfo(function (err, data) { if (err) throw err; console.log('*** Metadata found in the MBTiles'); console.log(data); @@ -34,14 +34,14 @@ module.exports = { }, listen: function (config, tiles, onListen) { - app.get('/', (req, res) => { + app.get('/', function (req, res) { res.render('map', config); }); - app.get('/debug/:z/:x/:y.pbf', (req, res) => { + app.get('/debug/:z/:x/:y.pbf', function (req, res) { var p = req.params; console.log('Serving', p.z + '/' + p.x + '/' + p.y); - tiles.getTile(p.z, p.x, p.y, (err, tile, headers) => { + tiles.getTile(p.z, p.x, p.y, function (err, tile, headers) { if (err) { console.error(err); res.end();