fix formatting issues
This commit is contained in:
parent
f07e6ef3e8
commit
a007218e2d
3 changed files with 97 additions and 97 deletions
|
@ -9,13 +9,13 @@ var clone = require('clone'),
|
||||||
express = require('express');
|
express = require('express');
|
||||||
|
|
||||||
|
|
||||||
module.exports = function (options, repo, params, id, reportTiles, reportFont) {
|
module.exports = function(options, repo, params, id, reportTiles, reportFont) {
|
||||||
var app = express().disable('x-powered-by');
|
var app = express().disable('x-powered-by');
|
||||||
|
|
||||||
var styleFile = path.resolve(options.paths.styles, params.style);
|
var styleFile = path.resolve(options.paths.styles, params.style);
|
||||||
|
|
||||||
var styleJSON = clone(require(styleFile));
|
var styleJSON = clone(require(styleFile));
|
||||||
Object.keys(styleJSON.sources).forEach(function (name) {
|
Object.keys(styleJSON.sources).forEach(function(name) {
|
||||||
var source = styleJSON.sources[name];
|
var source = styleJSON.sources[name];
|
||||||
var url = source.url;
|
var url = source.url;
|
||||||
if (url && url.lastIndexOf('mbtiles:', 0) === 0) {
|
if (url && url.lastIndexOf('mbtiles:', 0) === 0) {
|
||||||
|
@ -35,7 +35,7 @@ module.exports = function (options, repo, params, id, reportTiles, reportFont) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
styleJSON.layers.forEach(function (obj) {
|
styleJSON.layers.forEach(function(obj) {
|
||||||
if (obj['type'] == 'symbol') {
|
if (obj['type'] == 'symbol') {
|
||||||
var fonts = (obj['layout'] || {})['text-font'];
|
var fonts = (obj['layout'] || {})['text-font'];
|
||||||
if (fonts && fonts.length) {
|
if (fonts && fonts.length) {
|
||||||
|
@ -115,7 +115,7 @@ module.exports = function (options, repo, params, id, reportTiles, reportFont) {
|
||||||
};
|
};
|
||||||
|
|
||||||
var styleJSON_ = clone(styleJSON);
|
var styleJSON_ = clone(styleJSON);
|
||||||
Object.keys(styleJSON_.sources).forEach(function (name) {
|
Object.keys(styleJSON_.sources).forEach(function(name) {
|
||||||
var source = styleJSON_.sources[name];
|
var source = styleJSON_.sources[name];
|
||||||
source.url = fixUrl(source.url);
|
source.url = fixUrl(source.url);
|
||||||
});
|
});
|
||||||
|
|
|
@ -84,7 +84,7 @@ function start(opts) {
|
||||||
|
|
||||||
var startupPromises = [];
|
var startupPromises = [];
|
||||||
|
|
||||||
var checkPath = function (type) {
|
var checkPath = function(type) {
|
||||||
if (!fs.existsSync(paths[type])) {
|
if (!fs.existsSync(paths[type])) {
|
||||||
console.error('The specified path for "' + type + '" does not exist (' + paths[type] + ').');
|
console.error('The specified path for "' + type + '" does not exist (' + paths[type] + ').');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -98,7 +98,7 @@ function start(opts) {
|
||||||
if (options.dataDecorator) {
|
if (options.dataDecorator) {
|
||||||
try {
|
try {
|
||||||
options.dataDecoratorFunc = require(path.resolve(paths.root, options.dataDecorator));
|
options.dataDecoratorFunc = require(path.resolve(paths.root, options.dataDecorator));
|
||||||
} catch (e) { }
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = clone(config.data || {});
|
var data = clone(config.data || {});
|
||||||
|
@ -107,7 +107,7 @@ function start(opts) {
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(config.styles || {}).forEach(function (id) {
|
Object.keys(config.styles || {}).forEach(function(id) {
|
||||||
var item = config.styles[id];
|
var item = config.styles[id];
|
||||||
if (!item.style || item.style.length == 0) {
|
if (!item.style || item.style.length == 0) {
|
||||||
console.log('Missing "style" property for ' + id);
|
console.log('Missing "style" property for ' + id);
|
||||||
|
@ -116,9 +116,9 @@ function start(opts) {
|
||||||
|
|
||||||
if (item.serve_data !== false) {
|
if (item.serve_data !== false) {
|
||||||
startupPromises.push(serve_style(options, serving.styles, item, id,
|
startupPromises.push(serve_style(options, serving.styles, item, id,
|
||||||
function (mbtiles, fromData) {
|
function(mbtiles, fromData) {
|
||||||
var dataItemId;
|
var dataItemId;
|
||||||
Object.keys(data).forEach(function (id) {
|
Object.keys(data).forEach(function(id) {
|
||||||
if (fromData) {
|
if (fromData) {
|
||||||
if (id == mbtiles) {
|
if (id == mbtiles) {
|
||||||
dataItemId = id;
|
dataItemId = id;
|
||||||
|
@ -142,9 +142,9 @@ function start(opts) {
|
||||||
};
|
};
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
}, function (font) {
|
}, function(font) {
|
||||||
serving.fonts[font] = true;
|
serving.fonts[font] = true;
|
||||||
}).then(function (sub) {
|
}).then(function(sub) {
|
||||||
app.use('/styles/', sub);
|
app.use('/styles/', sub);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
@ -152,16 +152,16 @@ function start(opts) {
|
||||||
if (serve_rendered) {
|
if (serve_rendered) {
|
||||||
startupPromises.push(
|
startupPromises.push(
|
||||||
serve_rendered(options, serving.rendered, item, id,
|
serve_rendered(options, serving.rendered, item, id,
|
||||||
function (mbtiles) {
|
function(mbtiles) {
|
||||||
var mbtilesFile;
|
var mbtilesFile;
|
||||||
Object.keys(data).forEach(function (id) {
|
Object.keys(data).forEach(function(id) {
|
||||||
if (id == mbtiles) {
|
if (id == mbtiles) {
|
||||||
mbtilesFile = data[id].mbtiles;
|
mbtilesFile = data[id].mbtiles;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return mbtilesFile;
|
return mbtilesFile;
|
||||||
}
|
}
|
||||||
).then(function (sub) {
|
).then(function(sub) {
|
||||||
app.use('/styles/', sub);
|
app.use('/styles/', sub);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -172,12 +172,12 @@ function start(opts) {
|
||||||
});
|
});
|
||||||
|
|
||||||
startupPromises.push(
|
startupPromises.push(
|
||||||
serve_font(options, serving.fonts).then(function (sub) {
|
serve_font(options, serving.fonts).then(function(sub) {
|
||||||
app.use('/', sub);
|
app.use('/', sub);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
Object.keys(data).forEach(function (id) {
|
Object.keys(data).forEach(function(id) {
|
||||||
var item = data[id];
|
var item = data[id];
|
||||||
if (!item.mbtiles || item.mbtiles.length == 0) {
|
if (!item.mbtiles || item.mbtiles.length == 0) {
|
||||||
console.log('Missing "mbtiles" property for ' + id);
|
console.log('Missing "mbtiles" property for ' + id);
|
||||||
|
@ -185,16 +185,16 @@ function start(opts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
startupPromises.push(
|
startupPromises.push(
|
||||||
serve_data(options, serving.data, item, id, serving.styles).then(function (sub) {
|
serve_data(options, serving.data, item, id, serving.styles).then(function(sub) {
|
||||||
app.use('/data/', sub);
|
app.use('/data/', sub);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/styles.json', function (req, res, next) {
|
app.get('/styles.json', function(req, res, next) {
|
||||||
var result = [];
|
var result = [];
|
||||||
var query = req.query.key ? ('?key=' + req.query.key) : '';
|
var query = req.query.key ? ('?key=' + req.query.key) : '';
|
||||||
Object.keys(serving.styles).forEach(function (id) {
|
Object.keys(serving.styles).forEach(function(id) {
|
||||||
var styleJSON = serving.styles[id];
|
var styleJSON = serving.styles[id];
|
||||||
result.push({
|
result.push({
|
||||||
version: styleJSON.version,
|
version: styleJSON.version,
|
||||||
|
@ -207,8 +207,8 @@ function start(opts) {
|
||||||
res.send(result);
|
res.send(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
var addTileJSONs = function (arr, req, type) {
|
var addTileJSONs = function(arr, req, type) {
|
||||||
Object.keys(serving[type]).forEach(function (id) {
|
Object.keys(serving[type]).forEach(function(id) {
|
||||||
var info = clone(serving[type][id]);
|
var info = clone(serving[type][id]);
|
||||||
var path = '';
|
var path = '';
|
||||||
if (type == 'rendered') {
|
if (type == 'rendered') {
|
||||||
|
@ -224,13 +224,13 @@ function start(opts) {
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
|
|
||||||
app.get('/rendered.json', function (req, res, next) {
|
app.get('/rendered.json', function(req, res, next) {
|
||||||
res.send(addTileJSONs([], req, 'rendered'));
|
res.send(addTileJSONs([], req, 'rendered'));
|
||||||
});
|
});
|
||||||
app.get('/data.json', function (req, res, next) {
|
app.get('/data.json', function(req, res, next) {
|
||||||
res.send(addTileJSONs([], req, 'data'));
|
res.send(addTileJSONs([], req, 'data'));
|
||||||
});
|
});
|
||||||
app.get('/index.json', function (req, res, next) {
|
app.get('/index.json', function(req, res, next) {
|
||||||
res.send(addTileJSONs(addTileJSONs([], req, 'rendered'), req, 'data'));
|
res.send(addTileJSONs(addTileJSONs([], req, 'rendered'), req, 'data'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -239,7 +239,7 @@ function start(opts) {
|
||||||
app.use('/', express.static(path.join(__dirname, '../public/resources')));
|
app.use('/', express.static(path.join(__dirname, '../public/resources')));
|
||||||
|
|
||||||
var templates = path.join(__dirname, '../public/templates');
|
var templates = path.join(__dirname, '../public/templates');
|
||||||
var serveTemplate = function (urlPath, template, dataGetter) {
|
var serveTemplate = function(urlPath, template, dataGetter) {
|
||||||
var templateFile = templates + '/' + template + '.tmpl';
|
var templateFile = templates + '/' + template + '.tmpl';
|
||||||
if (template == 'index') {
|
if (template == 'index') {
|
||||||
if (options.frontPage === false) {
|
if (options.frontPage === false) {
|
||||||
|
@ -249,15 +249,15 @@ function start(opts) {
|
||||||
templateFile = path.resolve(paths.root, options.frontPage);
|
templateFile = path.resolve(paths.root, options.frontPage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
startupPromises.push(new Promise(function (resolve, reject) {
|
startupPromises.push(new Promise(function(resolve, reject) {
|
||||||
fs.readFile(templateFile, function (err, content) {
|
fs.readFile(templateFile, function(err, content) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Template not found:', err);
|
console.error('Template not found:', err);
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
var compiled = handlebars.compile(content.toString());
|
var compiled = handlebars.compile(content.toString());
|
||||||
|
|
||||||
app.use(urlPath, function (req, res, next) {
|
app.use(urlPath, function(req, res, next) {
|
||||||
var data = {};
|
var data = {};
|
||||||
if (dataGetter) {
|
if (dataGetter) {
|
||||||
data = dataGetter(req);
|
data = dataGetter(req);
|
||||||
|
@ -277,9 +277,9 @@ function start(opts) {
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
serveTemplate('/$', 'index', function (req) {
|
serveTemplate('/$', 'index', function(req) {
|
||||||
var styles = clone(config.styles || {});
|
var styles = clone(config.styles || {});
|
||||||
Object.keys(styles).forEach(function (id) {
|
Object.keys(styles).forEach(function(id) {
|
||||||
var style = styles[id];
|
var style = styles[id];
|
||||||
style.name = (serving.styles[id] || serving.rendered[id] || {}).name;
|
style.name = (serving.styles[id] || serving.rendered[id] || {}).name;
|
||||||
style.serving_data = serving.styles[id];
|
style.serving_data = serving.styles[id];
|
||||||
|
@ -309,7 +309,7 @@ function start(opts) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var data = clone(serving.data || {});
|
var data = clone(serving.data || {});
|
||||||
Object.keys(data).forEach(function (id) {
|
Object.keys(data).forEach(function(id) {
|
||||||
var data_ = data[id];
|
var data_ = data[id];
|
||||||
var center = data_.center;
|
var center = data_.center;
|
||||||
if (center) {
|
if (center) {
|
||||||
|
@ -357,7 +357,7 @@ function start(opts) {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
serveTemplate('/styles/:id/$', 'viewer', function (req) {
|
serveTemplate('/styles/:id/$', 'viewer', function(req) {
|
||||||
var id = req.params.id;
|
var id = req.params.id;
|
||||||
var style = clone((config.styles || {})[id]);
|
var style = clone((config.styles || {})[id]);
|
||||||
if (!style) {
|
if (!style) {
|
||||||
|
@ -376,7 +376,7 @@ function start(opts) {
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
serveTemplate('/data/:id/$', 'data', function (req) {
|
serveTemplate('/data/:id/$', 'data', function(req) {
|
||||||
var id = req.params.id;
|
var id = req.params.id;
|
||||||
var data = clone(serving.data[id]);
|
var data = clone(serving.data[id]);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
@ -388,11 +388,11 @@ function start(opts) {
|
||||||
});
|
});
|
||||||
|
|
||||||
var startupComplete = false;
|
var startupComplete = false;
|
||||||
var startupPromise = Promise.all(startupPromises).then(function () {
|
var startupPromise = Promise.all(startupPromises).then(function() {
|
||||||
console.log('Startup complete');
|
console.log('Startup complete');
|
||||||
startupComplete = true;
|
startupComplete = true;
|
||||||
});
|
});
|
||||||
app.get('/health', function (req, res, next) {
|
app.get('/health', function(req, res, next) {
|
||||||
if (startupComplete) {
|
if (startupComplete) {
|
||||||
return res.status(200).send('OK');
|
return res.status(200).send('OK');
|
||||||
} else {
|
} else {
|
||||||
|
@ -400,7 +400,7 @@ function start(opts) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var server = app.listen(process.env.PORT || opts.port, process.env.BIND || opts.bind, function () {
|
var server = app.listen(process.env.PORT || opts.port, process.env.BIND || opts.bind, function() {
|
||||||
var address = this.address().address;
|
var address = this.address().address;
|
||||||
if (address.indexOf('::') === 0) {
|
if (address.indexOf('::') === 0) {
|
||||||
address = '[' + address + ']'; // literal IPv6 address
|
address = '[' + address + ']'; // literal IPv6 address
|
||||||
|
@ -418,17 +418,17 @@ function start(opts) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (opts) {
|
module.exports = function(opts) {
|
||||||
var running = start(opts);
|
var running = start(opts);
|
||||||
|
|
||||||
process.on('SIGINT', function () {
|
process.on('SIGINT', function() {
|
||||||
process.exit();
|
process.exit();
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGHUP', function () {
|
process.on('SIGHUP', function() {
|
||||||
console.log('Stopping server and reloading config');
|
console.log('Stopping server and reloading config');
|
||||||
|
|
||||||
running.server.shutdown(function () {
|
running.server.shutdown(function() {
|
||||||
for (var key in require.cache) {
|
for (var key in require.cache) {
|
||||||
delete require.cache[key];
|
delete require.cache[key];
|
||||||
}
|
}
|
||||||
|
|
22
src/utils.js
22
src/utils.js
|
@ -6,7 +6,7 @@ var path = require('path'),
|
||||||
var clone = require('clone'),
|
var clone = require('clone'),
|
||||||
glyphCompose = require('glyph-pbf-composite');
|
glyphCompose = require('glyph-pbf-composite');
|
||||||
|
|
||||||
module.exports.getTileUrls = function (req, domains, path, format, aliases, options) {
|
module.exports.getTileUrls = function(req, domains, path, format, aliases, options) {
|
||||||
|
|
||||||
if (domains) {
|
if (domains) {
|
||||||
if (domains.constructor === String && domains.length > 0) {
|
if (domains.constructor === String && domains.length > 0) {
|
||||||
|
@ -17,7 +17,7 @@ module.exports.getTileUrls = function (req, domains, path, format, aliases, opti
|
||||||
var relativeSubdomainsUsable = hostParts.length > 1 &&
|
var relativeSubdomainsUsable = hostParts.length > 1 &&
|
||||||
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(host);
|
!/^([0-9]{1,3}\.){3}[0-9]{1,3}(\:[0-9]+)?$/.test(host);
|
||||||
var newDomains = [];
|
var newDomains = [];
|
||||||
domains.forEach(function (domain) {
|
domains.forEach(function(domain) {
|
||||||
if (domain.indexOf('*') !== -1) {
|
if (domain.indexOf('*') !== -1) {
|
||||||
if (relativeSubdomainsUsable) {
|
if (relativeSubdomainsUsable) {
|
||||||
var newParts = hostParts.slice(1);
|
var newParts = hostParts.slice(1);
|
||||||
|
@ -49,7 +49,7 @@ module.exports.getTileUrls = function (req, domains, path, format, aliases, opti
|
||||||
}
|
}
|
||||||
|
|
||||||
var uris = [];
|
var uris = [];
|
||||||
domains.forEach(function (domain) {
|
domains.forEach(function(domain) {
|
||||||
uris.push(req.protocol + '://' + domain + '/' + path +
|
uris.push(req.protocol + '://' + domain + '/' + path +
|
||||||
'/{z}/{x}/{y}.' + format + query);
|
'/{z}/{x}/{y}.' + format + query);
|
||||||
});
|
});
|
||||||
|
@ -57,7 +57,7 @@ module.exports.getTileUrls = function (req, domains, path, format, aliases, opti
|
||||||
return uris;
|
return uris;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.fixTileJSONCenter = function (tileJSON) {
|
module.exports.fixTileJSONCenter = function(tileJSON) {
|
||||||
if (tileJSON.bounds && !tileJSON.center) {
|
if (tileJSON.bounds && !tileJSON.center) {
|
||||||
var fitWidth = 1024;
|
var fitWidth = 1024;
|
||||||
var tiles = fitWidth / 256;
|
var tiles = fitWidth / 256;
|
||||||
|
@ -72,15 +72,15 @@ module.exports.fixTileJSONCenter = function (tileJSON) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var getFontPbf = function (allowedFonts, fontPath, name, range, fallbacks) {
|
var getFontPbf = function(allowedFonts, fontPath, name, range, fallbacks) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
||||||
var filename = path.join(fontPath, name, range + '.pbf');
|
var filename = path.join(fontPath, name, range + '.pbf');
|
||||||
if (!fallbacks) {
|
if (!fallbacks) {
|
||||||
fallbacks = clone(allowedFonts || {});
|
fallbacks = clone(allowedFonts || {});
|
||||||
}
|
}
|
||||||
delete fallbacks[name];
|
delete fallbacks[name];
|
||||||
fs.readFile(filename, function (err, data) {
|
fs.readFile(filename, function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('ERROR: Font not found:', name);
|
console.error('ERROR: Font not found:', name);
|
||||||
if (fallbacks && Object.keys(fallbacks).length) {
|
if (fallbacks && Object.keys(fallbacks).length) {
|
||||||
|
@ -101,17 +101,17 @@ var getFontPbf = function (allowedFonts, fontPath, name, range, fallbacks) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.getFontsPbf = function (allowedFonts, fontPath, names, range, fallbacks) {
|
module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fallbacks) {
|
||||||
var fonts = names.split(',');
|
var fonts = names.split(',');
|
||||||
var queue = [];
|
var queue = [];
|
||||||
fonts.forEach(function (font) {
|
fonts.forEach(function(font) {
|
||||||
queue.push(
|
queue.push(
|
||||||
getFontPbf(allowedFonts, fontPath, font, range, clone(allowedFonts || fallbacks))
|
getFontPbf(allowedFonts, fontPath, font, range, clone(allowedFonts || fallbacks))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
Promise.all(queue).then(function (values) {
|
Promise.all(queue).then(function(values) {
|
||||||
return resolve(glyphCompose.combine(values));
|
return resolve(glyphCompose.combine(values));
|
||||||
}, reject);
|
}, reject);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue