Browse Source

Merge pull request #268 from pelias/attribution

Fix attribution link
pull/275/head
Diana Shkolnikov 9 years ago
parent
commit
5e50e6b449
  1. 42
      middleware/geocodeJSON.js
  2. 19
      routes/v1.js

42
middleware/geocodeJSON.js

@ -1,18 +1,42 @@
var url = require('url');
var extend = require('extend'); var extend = require('extend');
var geojsonify = require('../helper/geojsonify').search; var geojsonify = require('../helper/geojsonify').search;
function setup(peliasConfig) { /**
* Returns a middleware function that converts elasticsearch
peliasConfig = peliasConfig || require( 'pelias-config' ).generate().api; * 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) { function middleware(req, res, next) {
return convertToGeocodeJSON(peliasConfig, req, res, next); return convertToGeocodeJSON(req, res, next, opts);
} }
return middleware; 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: {} }; 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, // 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. // and then multiple attributions, can be an object with one key by source.
// Can be a URI on the server, which outlines attribution details. // 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 // OPTIONAL. Default: null. The query that has been issued to trigger the
// search. // search.
@ -38,7 +62,7 @@ function convertToGeocodeJSON(peliasConfig, req, res, next) {
// OPTIONAL // OPTIONAL
// Freeform // Freeform
addEngine(peliasConfig, res.body.geocoding); addEngine(opts.config.version, res.body.geocoding);
// response envelope // response envelope
res.body.geocoding.timestamp = new Date().getTime(); 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 = { geocoding.engine = {
name: 'Pelias', name: 'Pelias',
author: 'Mapzen', author: 'Mapzen',
version: peliasConfig.version version: version
}; };
} }

19
routes/v1.js

@ -42,6 +42,8 @@ var postProc = {
*/ */
function addRoutes(app, peliasConfig) { function addRoutes(app, peliasConfig) {
var base = '/v1/';
/** ------------------------- routers ------------------------- **/ /** ------------------------- routers ------------------------- **/
var routers = { var routers = {
@ -57,7 +59,7 @@ function addRoutes(app, peliasConfig) {
controllers.search(), controllers.search(),
postProc.confidenceScores(peliasConfig), postProc.confidenceScores(peliasConfig),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig), postProc.geocodeJSON(peliasConfig, base),
postProc.sendJSON postProc.sendJSON
]), ]),
autocomplete: createRouter([ autocomplete: createRouter([
@ -66,7 +68,7 @@ function addRoutes(app, peliasConfig) {
controllers.search(null, require('../query/autocomplete')), controllers.search(null, require('../query/autocomplete')),
postProc.confidenceScores(peliasConfig), postProc.confidenceScores(peliasConfig),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig), postProc.geocodeJSON(peliasConfig, base),
postProc.sendJSON postProc.sendJSON
]), ]),
reverse: createRouter([ reverse: createRouter([
@ -76,14 +78,14 @@ function addRoutes(app, peliasConfig) {
// TODO: add confidence scores // TODO: add confidence scores
postProc.distances(), postProc.distances(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig), postProc.geocodeJSON(peliasConfig, base),
postProc.sendJSON postProc.sendJSON
]), ]),
place: createRouter([ place: createRouter([
sanitisers.place.middleware, sanitisers.place.middleware,
controllers.place(), controllers.place(),
postProc.renamePlacenames(), postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig), postProc.geocodeJSON(peliasConfig, base),
postProc.sendJSON postProc.sendJSON
]), ]),
status: createRouter([ status: createRouter([
@ -92,18 +94,19 @@ function addRoutes(app, peliasConfig) {
}; };
var base = '/v1/'; // static data endpoints
// api root
app.get ( base, routers.index ); app.get ( base, routers.index );
app.get ( base + 'attribution', routers.attribution ); 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 + 'place', routers.place );
app.get ( base + 'autocomplete', routers.autocomplete ); app.get ( base + 'autocomplete', routers.autocomplete );
app.get ( base + 'search', routers.search ); app.get ( base + 'search', routers.search );
app.post( base + 'search', routers.search ); app.post( base + 'search', routers.search );
app.get ( base + 'reverse', routers.reverse ); app.get ( base + 'reverse', routers.reverse );
app.get ( '/status', routers.status );
} }
/** /**

Loading…
Cancel
Save