diff --git a/src/serve_font.js b/src/serve_font.js index b102b1b..49e5b71 100644 --- a/src/serve_font.js +++ b/src/serve_font.js @@ -8,16 +8,16 @@ var clone = require('clone'), express = require('express'); -module.exports = function(fontPath, allowedFonts) { +module.exports = function(options, allowedFonts) { 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) { // if some of the files failed to load (does not exist or not allowed), // return empty buffer so the other fonts can still work if (allowedFonts[name]) { - var filename = rootPath + '/' + name + '/' + range + '.pbf'; + var filename = path.join(fontPath, name, range + '.pbf'); return fs.readFile(filename, function(err, data) { if (err) { console.log('Font load error:', filename); diff --git a/src/serve_raster.js b/src/serve_raster.js index a410327..2056e79 100644 --- a/src/serve_raster.js +++ b/src/serve_raster.js @@ -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 rootPath = path.join(process.cwd(), options.root || ''); + var rootPath = options.paths.root; - var styleUrl = options.style; + var styleFile = params.style; var map = { renderers: [], sources: {} @@ -50,8 +50,10 @@ module.exports = function(repo, options, id) { request: function(req, callback) { var protocol = req.url.split(':')[0]; //console.log('Handling request:', req); - if (protocol == req.url) { - fs.readFile(path.join(rootPath, unescape(req.url)), function(err, data) { + if (protocol == 'sprites' || protocol == 'fonts') { + 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 }); }); } 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 = { 'tilejson': '2.0.0', @@ -135,10 +139,10 @@ module.exports = function(repo, options, id) { 'maxzoom': 20, 'bounds': [-180, -85.0511, 180, 85.0511], 'format': 'png', - 'type': 'baselayer', - 'tiles': options.domains + 'type': 'baselayer' }; - Object.assign(tileJSON, options.tilejson || {}); + Object.assign(tileJSON, params.tilejson || {}); + tileJSON.tiles = params.domains || options.domains; var queue = []; Object.keys(styleJSON.sources).forEach(function(name) { @@ -149,8 +153,9 @@ module.exports = function(repo, options, id) { delete source.url; queue.push(function(callback) { - var mbtilesUrl = url.substring('mbtiles://'.length); - map.sources[name] = new mbtiles(path.join(rootPath, mbtilesUrl), function(err) { + var mbtilesFile = url.substring('mbtiles://'.length); + map.sources[name] = new mbtiles( + path.join(options.paths.mbtiles, mbtilesFile), function(err) { map.sources[name].getInfo(function(err, info) { Object.assign(source, info); source.basename = name; diff --git a/src/serve_style.js b/src/serve_style.js index c2fbad1..5f7dfb1 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -7,14 +7,12 @@ var clone = require('clone'), 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 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(styleUrl)); + var styleJSON = clone(require(styleFile)); Object.keys(styleJSON.sources).forEach(function(name) { var source = styleJSON.sources[name]; var url = source.url; @@ -39,7 +37,8 @@ module.exports = function(repo, options, id, reportVector, reportFont) { }; 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.glyphs = 'local://fonts/{fontstack}/{range}.pbf'; diff --git a/src/serve_vector.js b/src/serve_vector.js index 21210cf..f123a09 100644 --- a/src/serve_vector.js +++ b/src/serve_vector.js @@ -9,19 +9,18 @@ var clone = require('clone'), 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 rootPath = path.join(process.cwd(), options.root || ''); - - var mbtilesPath = options.mbtiles; + var mbtilesFile = params.mbtiles; var tileJSON = { - 'tiles': options.domains + 'tiles': params.domains || options.domains }; 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) { tileJSON['name'] = id; @@ -31,7 +30,7 @@ module.exports = function(repo, options, id) { tileJSON['basename'] = id; tileJSON['format'] = 'pbf'; - Object.assign(tileJSON, options.tilejson || {}); + Object.assign(tileJSON, params.tilejson || {}); }); }); diff --git a/src/server.js b/src/server.js index 62796ad..89c73f9 100644 --- a/src/server.js +++ b/src/server.js @@ -42,6 +42,15 @@ module.exports = function(opts, callback) { var configPath = path.resolve(opts.config), 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); Object.keys(config.styles || {}).forEach(function(id) { @@ -52,7 +61,7 @@ module.exports = function(opts, callback) { } if (item.vector !== false) { - app.use('/', serve_style(serving.styles, item, id, + app.use('/', serve_style(options, serving.styles, item, id, function(mbtiles) { var vectorItemId; Object.keys(vector).forEach(function(id) { @@ -75,13 +84,13 @@ module.exports = function(opts, callback) { })); } 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) { // serve fonts only if serving some styles - app.use('/', serve_font('glyphs', serving.fonts)); + app.use('/', serve_font(options, serving.fonts)); } //TODO: cors @@ -93,7 +102,7 @@ module.exports = function(opts, callback) { 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) {