Commit 6fa91cf3 authored by Rodrigo Tapia-McClung's avatar Rodrigo Tapia-McClung

Serve gzipped tiles from PostGIS

parent 9ea9f68e
...@@ -6,6 +6,8 @@ const merc = new sm({ ...@@ -6,6 +6,8 @@ const merc = new sm({
size: 256 size: 256
}) })
const zlib = require('zlib');
function toBoolean(value) { function toBoolean(value) {
if (!value) { if (!value) {
return false; return false;
...@@ -74,6 +76,13 @@ const sql = (params, query) => { ...@@ -74,6 +76,13 @@ const sql = (params, query) => {
` `
} // TODO, use sql place holders $1, $2 etc. instead of inserting user-parameters into query } // TODO, use sql place holders $1, $2 etc. instead of inserting user-parameters into query
let header = {
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept",
"Content-Type":"application/x-protobuf",
"Content-Encoding":"gzip"
};
module.exports = function(app, pool, cache) { module.exports = function(app, pool, cache) {
let cacheMiddleWare = async(req, res, next) => { let cacheMiddleWare = async(req, res, next) => {
...@@ -91,7 +100,9 @@ module.exports = function(app, pool, cache) { ...@@ -91,7 +100,9 @@ module.exports = function(app, pool, cache) {
if (mvt.length === 0) { if (mvt.length === 0) {
res.status(204) res.status(204)
} }
res.header('Content-Type', 'application/x-protobuf').send(mvt); //res.header('Content-Type', 'application/x-protobuf').send(mvt);
// send cached gzipped tiles with appropriate header
res.header(header).send(mvt);
return; return;
} else { } else {
res.sendResponse = res.send; res.sendResponse = res.send;
...@@ -170,7 +181,11 @@ module.exports = function(app, pool, cache) { ...@@ -170,7 +181,11 @@ module.exports = function(app, pool, cache) {
if (mvt.length === 0) { if (mvt.length === 0) {
res.status(204) res.status(204)
} }
res.header('Content-Type', 'application/x-protobuf').send(mvt); //res.header('Content-Type', 'application/x-protobuf').send(mvt);
// gzip and send tiles
zlib.gzip(mvt, function(err, pbf) {
res.header(header).send(pbf);
});
} catch(err) { } catch(err) {
console.log(err); console.log(err);
let status = 500; let status = 500;
......
...@@ -6044,6 +6044,11 @@ ...@@ -6044,6 +6044,11 @@
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI="
},
"zlib": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zlib/-/zlib-1.0.5.tgz",
"integrity": "sha1-bnyXL8NxxkWmr7A6sUdp3vEU/MA="
} }
} }
} }
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
"morgan": "^1.9.1", "morgan": "^1.9.1",
"nodemon": "^1.19.2", "nodemon": "^1.19.2",
"path": "^0.12.7", "path": "^0.12.7",
"pg": "^7.12.1" "pg": "^7.12.1",
"zlib": "^1.0.5"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment