From 73419342a605f3de18d119f149866973f0ccda4a Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Wed, 16 Sep 2015 12:52:18 -0400 Subject: [PATCH 1/2] Fix attribution link --- middleware/geocodeJSON.js | 42 ++++++++++++++++++++++++++++++--------- routes/v1.js | 19 ++++++++++-------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index 4f7943e4..5ef37765 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -1,18 +1,42 @@ +var url = require('url'); var extend = require('extend'); var geojsonify = require('../helper/geojsonify').search; -function setup(peliasConfig) { - - peliasConfig = peliasConfig || require( 'pelias-config' ).generate().api; +/** + * Returns a middleware function that converts elasticsearch + * results into geocodeJSON format. + * + * @param {object} [peliasConfig] api portion of pelias config + * @param {string} [basePath] + * @returns {middleware} + */ +function setup(peliasConfig, basePath) { + + var opts = { + config: peliasConfig || require('pelias-config').generate().api, + basePath: basePath || '/' + }; function middleware(req, res, next) { - return convertToGeocodeJSON(peliasConfig, req, next); + return convertToGeocodeJSON(req, res, next, opts); } return middleware; } -function convertToGeocodeJSON(peliasConfig, req, next) { +/** + * Converts elasticsearch results into geocodeJSON format + * + * @param {object} req + * @param {object} res + * @param {object} next + * @param {object} opts + * @param {string} opts.basePath e.g. '/v1/' + * @param {string} opts.config.host e.g. 'pelias.mapzen.com' + * @param {string} opts.config.version e.g. 1.0 + * @returns {*} + */ +function convertToGeocodeJSON(req, res, next, opts) { // do nothing if no result data set if (!req.results || !req.results.data) { @@ -28,7 +52,7 @@ function convertToGeocodeJSON(peliasConfig, req, next) { // OPTIONAL. Default: null. The attribution of the data. In case of multiple sources, // and then multiple attributions, can be an object with one key by source. // Can be a URI on the server, which outlines attribution details. - req.results.geojson.geocoding.attribution = peliasConfig.host + 'attribution'; + req.results.geojson.geocoding.attribution = url.resolve(opts.config.host, opts.basePath + 'attribution'); // OPTIONAL. Default: null. The query that has been issued to trigger the // search. @@ -43,7 +67,7 @@ function convertToGeocodeJSON(peliasConfig, req, next) { // OPTIONAL // Freeform - addEngine(peliasConfig, req.results.geojson.geocoding); + addEngine(opts.config.version, req.results.geojson.geocoding); // response envelope req.results.geojson.geocoding.timestamp = new Date().getTime(); @@ -61,11 +85,11 @@ function addMessages(results, msgType, geocoding) { } } -function addEngine(peliasConfig, geocoding) { +function addEngine(version, geocoding) { geocoding.engine = { name: 'Pelias', author: 'Mapzen', - version: peliasConfig.version + version: version }; } diff --git a/routes/v1.js b/routes/v1.js index 97ddcc0b..a9b61c3d 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -42,6 +42,8 @@ var postProc = { */ function addRoutes(app, peliasConfig) { + var base = '/v1/'; + /** ------------------------- routers ------------------------- **/ var routers = { @@ -57,7 +59,7 @@ function addRoutes(app, peliasConfig) { controllers.search(), postProc.confidenceScores(peliasConfig), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), autocomplete: createRouter([ @@ -66,7 +68,7 @@ function addRoutes(app, peliasConfig) { controllers.search(null, require('../query/autocomplete')), postProc.confidenceScores(peliasConfig), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), reverse: createRouter([ @@ -76,14 +78,14 @@ function addRoutes(app, peliasConfig) { // TODO: add confidence scores postProc.distances(), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), place: createRouter([ sanitisers.place.middleware, controllers.place(), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), status: createRouter([ @@ -92,18 +94,19 @@ function addRoutes(app, peliasConfig) { }; - var base = '/v1/'; - - // api root + // static data endpoints app.get ( base, routers.index ); app.get ( base + 'attribution', routers.attribution ); + app.get ( '/attribution', routers.attribution ); + app.get ( '/status', routers.status ); + + // backend dependent endpoints app.get ( base + 'place', routers.place ); app.get ( base + 'autocomplete', routers.autocomplete ); app.get ( base + 'search', routers.search ); app.post( base + 'search', routers.search ); app.get ( base + 'reverse', routers.reverse ); - app.get ( '/status', routers.status ); } /** From 9433d995b1eabe7dee63676099cfa509abc36b23 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Wed, 16 Sep 2015 12:52:18 -0400 Subject: [PATCH 2/2] Fix attribution link --- middleware/geocodeJSON.js | 42 ++++++++++++++++++++++++++++++--------- routes/v1.js | 19 ++++++++++-------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index 01caa01c..80d69e86 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -1,18 +1,42 @@ +var url = require('url'); var extend = require('extend'); var geojsonify = require('../helper/geojsonify').search; -function setup(peliasConfig) { - - peliasConfig = peliasConfig || require( 'pelias-config' ).generate().api; +/** + * Returns a middleware function that converts elasticsearch + * results into geocodeJSON format. + * + * @param {object} [peliasConfig] api portion of pelias config + * @param {string} [basePath] + * @returns {middleware} + */ +function setup(peliasConfig, basePath) { + + var opts = { + config: peliasConfig || require('pelias-config').generate().api, + basePath: basePath || '/' + }; function middleware(req, res, next) { - return convertToGeocodeJSON(peliasConfig, req, res, next); + return convertToGeocodeJSON(req, res, next, opts); } return middleware; } -function convertToGeocodeJSON(peliasConfig, req, res, next) { +/** + * Converts elasticsearch results into geocodeJSON format + * + * @param {object} req + * @param {object} res + * @param {object} next + * @param {object} opts + * @param {string} opts.basePath e.g. '/v1/' + * @param {string} opts.config.host e.g. 'pelias.mapzen.com' + * @param {string} opts.config.version e.g. 1.0 + * @returns {*} + */ +function convertToGeocodeJSON(req, res, next, opts) { res.body = { geocoding: {} }; @@ -23,7 +47,7 @@ function convertToGeocodeJSON(peliasConfig, req, res, next) { // OPTIONAL. Default: null. The attribution of the data. In case of multiple sources, // and then multiple attributions, can be an object with one key by source. // Can be a URI on the server, which outlines attribution details. - res.body.geocoding.attribution = peliasConfig.host + 'attribution'; + res.body.geocoding.attribution = url.resolve(opts.config.host, opts.basePath + 'attribution'); // OPTIONAL. Default: null. The query that has been issued to trigger the // search. @@ -38,7 +62,7 @@ function convertToGeocodeJSON(peliasConfig, req, res, next) { // OPTIONAL // Freeform - addEngine(peliasConfig, res.body.geocoding); + addEngine(opts.config.version, res.body.geocoding); // response envelope res.body.geocoding.timestamp = new Date().getTime(); @@ -55,11 +79,11 @@ function addMessages(req, msgType, geocoding) { } } -function addEngine(peliasConfig, geocoding) { +function addEngine(version, geocoding) { geocoding.engine = { name: 'Pelias', author: 'Mapzen', - version: peliasConfig.version + version: version }; } diff --git a/routes/v1.js b/routes/v1.js index 97ddcc0b..a9b61c3d 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -42,6 +42,8 @@ var postProc = { */ function addRoutes(app, peliasConfig) { + var base = '/v1/'; + /** ------------------------- routers ------------------------- **/ var routers = { @@ -57,7 +59,7 @@ function addRoutes(app, peliasConfig) { controllers.search(), postProc.confidenceScores(peliasConfig), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), autocomplete: createRouter([ @@ -66,7 +68,7 @@ function addRoutes(app, peliasConfig) { controllers.search(null, require('../query/autocomplete')), postProc.confidenceScores(peliasConfig), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), reverse: createRouter([ @@ -76,14 +78,14 @@ function addRoutes(app, peliasConfig) { // TODO: add confidence scores postProc.distances(), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), place: createRouter([ sanitisers.place.middleware, controllers.place(), postProc.renamePlacenames(), - postProc.geocodeJSON(peliasConfig), + postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), status: createRouter([ @@ -92,18 +94,19 @@ function addRoutes(app, peliasConfig) { }; - var base = '/v1/'; - - // api root + // static data endpoints app.get ( base, routers.index ); app.get ( base + 'attribution', routers.attribution ); + app.get ( '/attribution', routers.attribution ); + app.get ( '/status', routers.status ); + + // backend dependent endpoints app.get ( base + 'place', routers.place ); app.get ( base + 'autocomplete', routers.autocomplete ); app.get ( base + 'search', routers.search ); app.post( base + 'search', routers.search ); app.get ( base + 'reverse', routers.reverse ); - app.get ( '/status', routers.status ); } /**