-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libpostal): Use libpostal service
BREAKING CHANGE Use microservice-wrapper to avoid having to load libpostal locally. Note: this now requires a new configuration section in `pelias.json`, a top-level `services` key with the usual properties. Here's an example full `pelias.json`: ``` { "api": { "textAnalyzer": "libpostal" }, "services": { "libpostal": { "url": "http://libpostal-service-url:8080", "timeout": 4000 } } } ``` Fixes #106
- Loading branch information
1 parent
123a119
commit 6e786db
Showing
11 changed files
with
259 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const mock_libpostal = require('../test/lib/mock_libpostal'); | ||
|
||
// This module is a wrapper around the actual libpostal service library | ||
// and the mock libpostal library | ||
// it allows an environment variable to switch which library is used in application code | ||
|
||
let libpostal_module; | ||
function get_libpostal() { | ||
// return the mock library if MOCK_LIBPOSTAL env var is set | ||
if (process.env.MOCK_LIBPOSTAL) { | ||
return mock_libpostal; | ||
// otherwise return the actual service | ||
} else { | ||
// lazy load the libpostal module so that tests can skip configuring the service | ||
if (!libpostal_module) { | ||
libpostal_module = require( '../libpostal/service' ); | ||
} | ||
return libpostal_module; | ||
} | ||
} | ||
|
||
module.exports = get_libpostal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const microservice_wrapper = require('pelias-microservice-wrapper'); | ||
const pelias_config = require('pelias-config').generate(); | ||
|
||
const LibpostalServiceConfig = class extends microservice_wrapper.ServiceConfiguration { | ||
constructor(configBlob) { | ||
super('libpostal', configBlob); | ||
} | ||
getUrl(params) { | ||
return this.baseUrl + params.endpoint; | ||
} | ||
getParameters(params) { | ||
return { | ||
address: params.address | ||
}; | ||
} | ||
}; | ||
|
||
// use the 'services.libpostal' config entry if available, otherwise fall back to 'api.services.libpostal' | ||
const config_entry = pelias_config.get('services.libpostal') || pelias_config.get('api.services.libpostal'); | ||
|
||
if (!config_entry) { | ||
throw new Error('Libpostal configuration not found in `services.libpostal` or `api.services.libpostal`'); | ||
} | ||
|
||
// create an instance of the libpostal service | ||
const libpostal_service = microservice_wrapper.service( | ||
new LibpostalServiceConfig(config_entry) | ||
); | ||
|
||
// create an object that looks like the interface to `node-postal` but uses a remote service | ||
module.exports = { | ||
expand: { | ||
expand_address: function(param, callback) { | ||
const params = { | ||
endpoint: 'expand', | ||
address: param | ||
}; | ||
|
||
// the libpostal service will not handle an empty parameter | ||
// so return empty array immediately | ||
if (!param) { | ||
return callback(null, []); | ||
} | ||
libpostal_service(params, callback); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.