-
-
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
2764a70
commit e046fd9
Showing
5 changed files
with
74 additions
and
23 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
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,48 @@ | ||
// deasync is used to proved a sync-looking interface | ||
// to the async call to the libpostal service | ||
const deasync = require('deasync'); | ||
const microservice_wrapper = require('pelias-microservice-wrapper'); | ||
const pelias_config = require('pelias-config').generate(); | ||
|
||
if (!pelias_config.services || !pelias_config.services.libpostal || !pelias_config.services.libpostal.url) { | ||
throw new Error('The URL to the libpostal service must be set in the services.libpostal.url property of pelias.json'); | ||
} | ||
|
||
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 | ||
}; | ||
} | ||
}; | ||
|
||
// create an instance of the libpostal service, with a synchronous interface | ||
const libpostal_service = deasync(microservice_wrapper.service( | ||
new LibpostalServiceConfig(pelias_config.services.libpostal) | ||
)); | ||
|
||
// create an object that looks like the interface to `node-postal` but uses a remote service | ||
module.exports = { | ||
expand: { | ||
expand_address: function(param) { | ||
const params = { | ||
endpoint: 'expand', | ||
address: param | ||
}; | ||
|
||
// the libpostal service will not handle an empty parameter | ||
// so return empty array immediately | ||
if (!param) { | ||
return []; | ||
} | ||
const result = libpostal_service(params); | ||
return result; | ||
} | ||
} | ||
}; |
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