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 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
};
}

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