Major cleaning of paths and urls

This commit is contained in:
Petr Sloup 2016-03-11 10:50:33 +01:00
parent d1e33d04cb
commit 8a46bd8b88
5 changed files with 43 additions and 31 deletions

View file

@ -8,16 +8,16 @@ var clone = require('clone'),
express = require('express'); express = require('express');
module.exports = function(fontPath, allowedFonts) { module.exports = function(options, allowedFonts) {
var app = express().disable('x-powered-by'); var app = express().disable('x-powered-by');
var rootPath = path.join(process.cwd(), fontPath || ''); var fontPath = options.paths.fonts;
var getFontPbf = function(name, range, callback) { var getFontPbf = function(name, range, callback) {
// if some of the files failed to load (does not exist or not allowed), // if some of the files failed to load (does not exist or not allowed),
// return empty buffer so the other fonts can still work // return empty buffer so the other fonts can still work
if (allowedFonts[name]) { if (allowedFonts[name]) {
var filename = rootPath + '/' + name + '/' + range + '.pbf'; var filename = path.join(fontPath, name, range + '.pbf');
return fs.readFile(filename, function(err, data) { return fs.readFile(filename, function(err, data) {
if (err) { if (err) {
console.log('Font load error:', filename); console.log('Font load error:', filename);

View file

@ -31,12 +31,12 @@ mbgl.on('message', function(e) {
} }
}); });
module.exports = function(repo, options, id) { module.exports = function(options, repo, params, id) {
var app = express().disable('x-powered-by'); var app = express().disable('x-powered-by');
var rootPath = path.join(process.cwd(), options.root || ''); var rootPath = options.paths.root;
var styleUrl = options.style; var styleFile = params.style;
var map = { var map = {
renderers: [], renderers: [],
sources: {} sources: {}
@ -50,8 +50,10 @@ module.exports = function(repo, options, id) {
request: function(req, callback) { request: function(req, callback) {
var protocol = req.url.split(':')[0]; var protocol = req.url.split(':')[0];
//console.log('Handling request:', req); //console.log('Handling request:', req);
if (protocol == req.url) { if (protocol == 'sprites' || protocol == 'fonts') {
fs.readFile(path.join(rootPath, unescape(req.url)), function(err, data) { var dir = options.paths[protocol];
var file = unescape(req.url).substring(protocol.length + 3);
fs.readFile(path.join(dir, file), function(err, data) {
callback(err, { data: data }); callback(err, { data: data });
}); });
} else if (protocol == 'mbtiles') { } else if (protocol == 'mbtiles') {
@ -125,7 +127,9 @@ module.exports = function(repo, options, id) {
}); });
}; };
styleJSON = require(path.join(rootPath, styleUrl)); styleJSON = require(path.join(options.paths.styles, styleFile));
styleJSON.sprite = 'sprites://' + path.basename(styleFile, '.json');
styleJSON.glyphs = 'fonts://{fontstack}/{range}.pbf';
var tileJSON = { var tileJSON = {
'tilejson': '2.0.0', 'tilejson': '2.0.0',
@ -135,10 +139,10 @@ module.exports = function(repo, options, id) {
'maxzoom': 20, 'maxzoom': 20,
'bounds': [-180, -85.0511, 180, 85.0511], 'bounds': [-180, -85.0511, 180, 85.0511],
'format': 'png', 'format': 'png',
'type': 'baselayer', 'type': 'baselayer'
'tiles': options.domains
}; };
Object.assign(tileJSON, options.tilejson || {}); Object.assign(tileJSON, params.tilejson || {});
tileJSON.tiles = params.domains || options.domains;
var queue = []; var queue = [];
Object.keys(styleJSON.sources).forEach(function(name) { Object.keys(styleJSON.sources).forEach(function(name) {
@ -149,8 +153,9 @@ module.exports = function(repo, options, id) {
delete source.url; delete source.url;
queue.push(function(callback) { queue.push(function(callback) {
var mbtilesUrl = url.substring('mbtiles://'.length); var mbtilesFile = url.substring('mbtiles://'.length);
map.sources[name] = new mbtiles(path.join(rootPath, mbtilesUrl), function(err) { map.sources[name] = new mbtiles(
path.join(options.paths.mbtiles, mbtilesFile), function(err) {
map.sources[name].getInfo(function(err, info) { map.sources[name].getInfo(function(err, info) {
Object.assign(source, info); Object.assign(source, info);
source.basename = name; source.basename = name;

View file

@ -7,14 +7,12 @@ var clone = require('clone'),
express = require('express'); express = require('express');
module.exports = function(repo, options, id, reportVector, reportFont) { module.exports = function(options, repo, params, id, reportVector, reportFont) {
var app = express().disable('x-powered-by'); var app = express().disable('x-powered-by');
var rootPath = path.join(process.cwd(), options.root || ''); var styleFile = path.join(options.paths.styles, params.style);
var styleUrl = path.join(rootPath, options.style); var styleJSON = clone(require(styleFile));
var styleJSON = clone(require(styleUrl));
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;
@ -39,7 +37,8 @@ module.exports = function(repo, options, id, reportVector, reportFont) {
}; };
styleJSON.layers.forEach(findFontReferences); styleJSON.layers.forEach(findFontReferences);
var spritePath = path.join(rootPath, styleJSON.sprite); var spritePath = path.join(options.paths.sprites,
path.basename(styleFile, '.json'));
styleJSON.sprite = 'local://styles/' + id + '/sprite'; styleJSON.sprite = 'local://styles/' + id + '/sprite';
styleJSON.glyphs = 'local://fonts/{fontstack}/{range}.pbf'; styleJSON.glyphs = 'local://fonts/{fontstack}/{range}.pbf';

View file

@ -9,19 +9,18 @@ var clone = require('clone'),
var utils = require('./utils'); var utils = require('./utils');
module.exports = function(repo, options, id) { module.exports = function(options, repo, params, id) {
var app = express().disable('x-powered-by'); var app = express().disable('x-powered-by');
var rootPath = path.join(process.cwd(), options.root || ''); var mbtilesFile = params.mbtiles;
var mbtilesPath = options.mbtiles;
var tileJSON = { var tileJSON = {
'tiles': options.domains 'tiles': params.domains || options.domains
}; };
repo[id] = tileJSON; repo[id] = tileJSON;
var source = new mbtiles(path.join(rootPath, mbtilesPath), function(err) { var source = new mbtiles(path.join(options.paths.mbtiles, mbtilesFile),
function(err) {
source.getInfo(function(err, info) { source.getInfo(function(err, info) {
tileJSON['name'] = id; tileJSON['name'] = id;
@ -31,7 +30,7 @@ module.exports = function(repo, options, id) {
tileJSON['basename'] = id; tileJSON['basename'] = id;
tileJSON['format'] = 'pbf'; tileJSON['format'] = 'pbf';
Object.assign(tileJSON, options.tilejson || {}); Object.assign(tileJSON, params.tilejson || {});
}); });
}); });

View file

@ -42,6 +42,15 @@ module.exports = function(opts, callback) {
var configPath = path.resolve(opts.config), var configPath = path.resolve(opts.config),
config = require(configPath); config = require(configPath);
var options = config.options || {};
var paths = options.paths || {};
options.paths = paths;
paths.root = path.join(process.cwd(), paths.root || '');
paths.styles = path.join(paths.root, paths.styles || '');
paths.fonts = path.join(paths.root, paths.fonts || '');
paths.sprites = path.join(paths.root, paths.sprites || '');
paths.mbtiles = path.join(paths.root, paths.mbtiles || '');
var vector = clone(config.vector); var vector = clone(config.vector);
Object.keys(config.styles || {}).forEach(function(id) { Object.keys(config.styles || {}).forEach(function(id) {
@ -52,7 +61,7 @@ module.exports = function(opts, callback) {
} }
if (item.vector !== false) { if (item.vector !== false) {
app.use('/', serve_style(serving.styles, item, id, app.use('/', serve_style(options, serving.styles, item, id,
function(mbtiles) { function(mbtiles) {
var vectorItemId; var vectorItemId;
Object.keys(vector).forEach(function(id) { Object.keys(vector).forEach(function(id) {
@ -75,13 +84,13 @@ module.exports = function(opts, callback) {
})); }));
} }
if (item.raster !== false) { if (item.raster !== false) {
app.use('/', serve_raster(serving.raster, item, id)); app.use('/', serve_raster(options, serving.raster, item, id));
} }
}); });
if (Object.keys(serving.styles).length > 0) { if (Object.keys(serving.styles).length > 0) {
// serve fonts only if serving some styles // serve fonts only if serving some styles
app.use('/', serve_font('glyphs', serving.fonts)); app.use('/', serve_font(options, serving.fonts));
} }
//TODO: cors //TODO: cors
@ -93,7 +102,7 @@ module.exports = function(opts, callback) {
return; return;
} }
app.use('/', serve_vector(serving.vector, item, id)); app.use('/', serve_vector(options, serving.vector, item, id));
}); });
app.get('/styles.json', function(req, res, next) { app.get('/styles.json', function(req, res, next) {