Browse Source

Fix attribution link

pull/268/head^2
Diana Shkolnikov 9 years ago
parent
commit
73419342a6
  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 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
};
}

19
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 );
}
/**

Loading…
Cancel
Save