diff --git a/README.md b/README.md index a8bf41a28..997a8e97e 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ The API recognizes the following properties under the top-level `api` key in you |`services`|*no*||Service definitions for [point-in-polygon](https://github.com/pelias/pip-service), [libpostal](https://github.com/whosonfirst/go-whosonfirst-libpostal), [placeholder](https://github.com/pelias/placeholder), and [interpolation](https://github.com/pelias/interpolation) services. For a description of when different Pelias services are recommended or required, see our [services documentation](https://github.com/pelias/documentation/blob/master/services.md).| |`defaultParameters.focus.point.lon`
`defaultParameters.focus.point.lat`|no | |default coordinates for focus point |`targets.auto_discover`|no|true|Should `sources` and `layers` be automatically discovered by querying Elasticsearch at process startup. (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below). +|`targets.auto_discover_required`|no|false|If set to `true`, type mapping discovery failures will result in a fatal error. This means a valid connection to a working Elasticsearch cluster with a Pelias index is _required_ on startup. Setting this to true can help prevent issues such as incorrect configuration in production environments |`targets.layers_by_source`
`targets.source_aliases`
`targets.layer_aliases`|no | |custom values for which `sources` and `layers` the API accepts (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below). We recommend using the `targets.auto_discover:true` configuration instead of setting these manually. |`customBoosts` | no | `{}` | Allows configuring boosts for specific sources and layers, in order to influence result order. See [Configurable Boosts](#custom-boosts) below for details | |`autocomplete.exclude_address_length` | no | 0 | As a performance optimization, this optional filter excludes results from the 'address' layer for any queries where the character length of the 'subject' portion of the parsed_text is equal to or less than this many characters in length. Addresses are usually the bulk of the records in Elasticsearch, and searching across all of them for very short text inputs can be slow, with little benefit. Consider setting this to 1 or 2 if you have several million addresses in Pelias. | diff --git a/helper/TypeMapping.js b/helper/TypeMapping.js index 951dd6ba3..d81c49b10 100644 --- a/helper/TypeMapping.js +++ b/helper/TypeMapping.js @@ -126,16 +126,14 @@ TypeMapping.prototype.loadTargets = function( targetsBlock ){ // load values from either pelias config file or from elasticsearch TypeMapping.prototype.load = function( done ){ + // load targets from config file + this.loadFromConfig(); - // load pelias config const peliasConfigTargets = _.get( require('pelias-config').generate(require('../schema')), 'api.targets', {} ); - // load targets from config file - this.loadTargets( peliasConfigTargets ); - // do not load values from elasticsearch if( true !== peliasConfigTargets.auto_discover ){ if( 'function' === typeof done ){ done(); } @@ -146,6 +144,19 @@ TypeMapping.prototype.load = function( done ){ loadFromElasticsearch(this, done); }; +// load values from either pelias config file or from elasticsearch +TypeMapping.prototype.loadFromConfig = function( done ){ + + // load pelias config + const peliasConfigTargets = _.get( + require('pelias-config').generate(require('../schema')), + 'api.targets', {} + ); + + // load targets from config file + this.loadTargets( peliasConfigTargets ); +}; + // replace the contents of an object or array // while maintaining the original pointer reference function safeReplace(reference, replacement){ diff --git a/helper/type_mapping.js b/helper/type_mapping.js index 2e97231c2..b941aad94 100644 --- a/helper/type_mapping.js +++ b/helper/type_mapping.js @@ -1,8 +1,11 @@ const TypeMapping = require('./TypeMapping'); -// instantiate a new type mapping +// instantiate a singleton type mapping +// loading normal defaults from pelias-config happens now +// updating that config from Elasticsearch happens later +// before the webserver is started var tm = new TypeMapping(); -tm.load(); +tm.loadFromConfig(); // export singleton module.exports = tm; diff --git a/helper/type_mapping_discovery.js b/helper/type_mapping_discovery.js index a2239b494..72a7e8d36 100644 --- a/helper/type_mapping_discovery.js +++ b/helper/type_mapping_discovery.js @@ -45,7 +45,13 @@ module.exports = (tm, done) => { } // query error - if( err ){ logger.error( err ); } + if( err ){ + logger.error( err ); + + if (peliasConfig.get('api.targets.auto_discover_required') === true) { + process.exit(1); + } + } // invalid response else if ( totalHits < 1 ){ diff --git a/index.js b/index.js index ebdfede94..6b6cfb095 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,19 @@ const logger = require('pelias-logger').get('api'); +const type_mapping = require('./helper/type_mapping'); + const app = require('./app'), port = ( process.env.PORT || 3100 ), host = ( process.env.HOST || undefined ); -const server = app.listen( port, host, () => { - // ask server for the actual address and port its listening on - const listenAddress = server.address(); - logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` ); +let server; + +// load Elasticsearch type mappings before starting web server +type_mapping.load(() => { + server = app.listen( port, host, () => { + // ask server for the actual address and port its listening on + const listenAddress = server.address(); + logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` ); + }); }); function exitHandler() {