From d231727c4cd245c29025a1b5591faf5f0e93adf4 Mon Sep 17 00:00:00 2001 From: sylvain-morin Date: Wed, 15 Feb 2023 21:50:34 +0100 Subject: [PATCH 1/2] Refactor to avoid global.config --- lib/elastic_config.js | 12 ++++++++++++ lib/elastic_mapper.js | 37 ++++++++++++------------------------- lib/elastic_request.js | 29 +++++++++++++++-------------- lib/map_generator.js | 11 ++++++----- 4 files changed, 45 insertions(+), 44 deletions(-) create mode 100644 lib/elastic_config.js diff --git a/lib/elastic_config.js b/lib/elastic_config.js new file mode 100644 index 0000000..3b66820 --- /dev/null +++ b/lib/elastic_config.js @@ -0,0 +1,12 @@ +module.exports = { + config: { + elasticsearch: { + host: "localhost:9200", + searchIndex: `elasticmaps_${process.env.NODE_ENV || "development"}`, + geoPointField: "location" + }, + log: `elasticmaps.${process.env.NODE_ENV || "development"}.log`, + tileSize: 256, + debug: true + } +}; diff --git a/lib/elastic_mapper.js b/lib/elastic_mapper.js index cc7a505..f62c0e7 100644 --- a/lib/elastic_mapper.js +++ b/lib/elastic_mapper.js @@ -2,6 +2,7 @@ const express = require( "express" ); const querystring = require( "querystring" ); const _ = require( "lodash" ); const Geohash = require( "latlon-geohash" ); +const EC = require( "./elastic_config" ); const MapGenerator = require( "./map_generator" ); const ElasticRequest = require( "./elastic_request" ); @@ -117,7 +118,7 @@ ElasticMapper.csvFromResult = ( req, result ) => { } else if ( result.hits ) { target = result.hits.hits; } else { return []; } - const { geoPointField } = global.config.elasticsearch; + const { geoPointField } = EC.config.elasticsearch; const fieldsToMap = ( req.query.source && req.query.source.includes ) ? _.without( req.query.source.includes, geoPointField ) : []; @@ -213,7 +214,7 @@ ElasticMapper.route = async ( req, res ) => { } try { - const prepareQuery = global.config.prepareQuery || ElasticMapper.prepareQuery; + const prepareQuery = EC.config.prepareQuery || ElasticMapper.prepareQuery; await prepareQuery( req ); if ( req.includeTotalHits && req.params.dataType !== "postgis" ) { const cachedCount = await ElasticRequest.count( req, { }, this ); @@ -229,17 +230,17 @@ ElasticMapper.route = async ( req, res ) => { ElasticMapper.debug( `${result.took}ms / ${result.hits.total.value} results :: [${req.bbox}]` ); req.csvData = ElasticMapper.csvFromResult( req, result ); } - if ( global.config.prepareStyle && req.params.format !== "grid.json" ) { - await global.config.prepareStyle( req ); + if ( EC.config.prepareStyle && req.params.format !== "grid.json" ) { + await EC.config.prepareStyle( req ); } const { map, layer } = await MapGenerator.createMapTemplate( req ); await MapGenerator.finishMap( req, res, map, layer, req.csvData ); - if ( global.config.beforeSendResult ) { - await global.config.beforeSendResult( req, res ); + if ( EC.config.beforeSendResult ) { + await EC.config.beforeSendResult( req, res ); } req.endTime = new Date( ); ElasticMapper.renderResult( req, res, req.tileData ); - if ( global.config.debug ) { + if ( EC.config.debug ) { ElasticMapper.printRequestLog( req ); } } catch ( e ) { @@ -259,31 +260,17 @@ ElasticMapper.printRequestLog = req => { }; ElasticMapper.debug = text => { - if ( global.config.debug ) { + if ( EC.config.debug ) { console.log( text ); // eslint-disable-line no-console } }; ElasticMapper.server = ( config = { } ) => { - global.config = _.defaults( config, { - environment: config.NODE_ENV || process.env.NODE_ENV || "development", - tileSize: 256, - debug: !( config.debug === false ) - } ); - global.config.log = global.config.log - || `elasticmaps.${global.config.environment}.log`; - global.config.elasticsearch = _.defaults( - global.config.elasticsearch || { }, - { - host: "localhost:9200", - searchIndex: `elasticmaps_${global.config.environment}`, - geoPointField: "location" - } - ); + EC.config = _.merge( EC.config, config ); // create the server and the map route const server = express( ); - if ( global.config.prepareApp && _.isFunction( global.config.prepareApp ) ) { - global.config.prepareApp( server, config ); + if ( EC.config.prepareApp && _.isFunction( EC.config.prepareApp ) ) { + EC.config.prepareApp( server, config ); } return server; }; diff --git a/lib/elastic_request.js b/lib/elastic_request.js index b52ed7a..1c8d567 100644 --- a/lib/elastic_request.js +++ b/lib/elastic_request.js @@ -5,6 +5,7 @@ const _ = require( "lodash" ); const elasticsearch = require( "elasticsearch" ); const Cacheman = require( "recacheman" ); const crypto = require( "crypto" ); +const EC = require( "./elastic_config" ); const MapGenerator = require( "./map_generator" ); const ElasticRequest = { esClient: null }; @@ -17,12 +18,12 @@ const COUNT_CACHE_SECONDS = 60 * 60 * 24; // 1 day ElasticRequest.createClient = ( ) => { if ( ElasticRequest.esClient === null ) { const clientConfig = { log: false }; - if ( global.config.elasticsearch.hosts ) { - clientConfig.hosts = _.isArray( global.config.elasticsearch.hosts ) - ? global.config.elasticsearch.hosts - : global.config.elasticsearch.hosts.split( " " ); + if ( EC.config.elasticsearch.hosts ) { + clientConfig.hosts = _.isArray( EC.config.elasticsearch.hosts ) + ? EC.config.elasticsearch.hosts + : EC.config.elasticsearch.hosts.split( " " ); } else { - clientConfig.host = global.config.elasticsearch.host; + clientConfig.host = EC.config.elasticsearch.host; } ElasticRequest.esClient = new elasticsearch.Client( clientConfig ); } @@ -41,8 +42,8 @@ ElasticRequest.count = async function ( req, options = { } ) { const countMethod = async ( ) => { const response = await ElasticRequest.esClient.search( { - preference: global.config.elasticsearch.preference, - index: req.elastic_index || global.config.elasticsearch.searchIndex, + preference: EC.config.elasticsearch.preference, + index: req.elastic_index || EC.config.elasticsearch.searchIndex, body: query, track_total_hits: true, ...options @@ -133,8 +134,8 @@ ElasticRequest.search = async ( req, options = { } ) => { const query = { ...req.elastic_query }; ElasticRequest.createClient( ); return ElasticRequest.esClient.search( { - preference: global.config.elasticsearch.preference, - index: req.elastic_index || global.config.elasticsearch.searchIndex, + preference: EC.config.elasticsearch.preference, + index: req.elastic_index || EC.config.elasticsearch.searchIndex, body: query, ...options } ); @@ -179,7 +180,7 @@ ElasticRequest.boundingBoxFilter = ( bbox, smoothing ) => { }; } - const field = global.config.elasticsearch.geoPointField; + const field = EC.config.elasticsearch.geoPointField; const boundingBox = { }; boundingBox[field] = { bottom_left: [qbbox[0], qbbox[1]], @@ -214,7 +215,7 @@ ElasticRequest.geotileGridPrecision = ( zoom, offset ) => { }; ElasticRequest.defaultMapFields = ( ) => ( - ["id", global.config.elasticsearch.geoPointField] + ["id", EC.config.elasticsearch.geoPointField] ); ElasticRequest.defaultMapQuery = ( ) => ( @@ -243,7 +244,7 @@ ElasticRequest.geohashAggregation = req => { agg = { zoom1: { geotile_grid: { - field: global.config.elasticsearch.geoPointField, + field: EC.config.elasticsearch.geoPointField, size: 10000, precision: ElasticRequest.geotileGridPrecision( req.params.zoom, req.query ? Number( req.query.precision_offset ) : null @@ -255,7 +256,7 @@ ElasticRequest.geohashAggregation = req => { agg = { zoom1: { geohash_grid: { - field: global.config.elasticsearch.geoPointField, + field: EC.config.elasticsearch.geoPointField, size: 30000, precision: ElasticRequest.geohashPrecision( req.params.zoom, req.query ? Number( req.query.precision_offset ) : null @@ -286,7 +287,7 @@ ElasticRequest.torqueAggregation = req => { return { zoom1: { geohash_grid: { - field: global.config.elasticsearch.geoPointField, + field: EC.config.elasticsearch.geoPointField, size: 30000, precision: ElasticRequest.geohashPrecision( req.params.zoom, req.query ? Number( req.query.precision_offset ) : null diff --git a/lib/map_generator.js b/lib/map_generator.js index 18ac8af..32b1d4d 100644 --- a/lib/map_generator.js +++ b/lib/map_generator.js @@ -7,6 +7,7 @@ const path = require( "path" ); const flatten = require( "flat" ); const { promisify } = require( "util" ); const SphericalMercator = require( "@mapbox/sphericalmercator" ); +const EC = require( "./elastic_config" ); const Styles = require( "./styles" ); // register shapefile plugin @@ -23,7 +24,7 @@ MapGenerator.blankImage = fs.readFileSync( path.join( __dirname, "assets/blank.p MapGenerator.createMercator = ( ) => { if ( MapGenerator.merc === null ) { - MapGenerator.merc = new SphericalMercator( { size: global.config.tileSize } ); + MapGenerator.merc = new SphericalMercator( { size: EC.config.tileSize } ); } }; @@ -50,7 +51,7 @@ MapGenerator.postgisDatasource = req => { let zoom = parseInt( req.params.zoom, 10 ); if ( req.largeTiles ) { zoom -= 1; } const datasourceConfig = { - ...global.config.database, + ...EC.config.database, type: "postgis", table: req.postgis.query, simplify_geometries: true, @@ -89,12 +90,12 @@ MapGenerator.finishMap = async ( req, res, map, layer, features ) => { if ( memDS ) { layer.datasource = memDS; } } map.add_layer( layer ); - let { tileSize } = global.config; + let { tileSize } = EC.config; if ( req.largeTiles ) { tileSize *= 2; } const mapRender = promisify( map.render.bind( map ) ); if ( req.params.format === "grid.json" ) { fields = _.without( req.query.source.includes, - global.config.elasticsearch.geoPointField ); + EC.config.elasticsearch.geoPointField ); // geohash aggregations will have cellCount if ( req.elastic_query.aggregations && req.elastic_query.aggregations.zoom1 ) { fields.push( "cellCount" ); @@ -135,7 +136,7 @@ MapGenerator.mapXML = specificStyle => ( MapGenerator.createMapTemplate = async req => { req.style = req.style || Styles.points( ); - let { tileSize } = global.config; + let { tileSize } = EC.config; if ( req.largeTiles ) { tileSize *= 2; } if ( req.params.format === "grid.json" ) { tileSize /= 2; From e74ddb9b55aa16ae99c7de5cb57105bef46e910b Mon Sep 17 00:00:00 2001 From: sylvain-morin Date: Wed, 15 Feb 2023 21:57:43 +0100 Subject: [PATCH 2/2] Refactor to avoid global.config --- test/elastic_mapper.js | 6 +++--- test/lib/helpers.js | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/test/elastic_mapper.js b/test/elastic_mapper.js index 5347451..27f5b14 100644 --- a/test/elastic_mapper.js +++ b/test/elastic_mapper.js @@ -1,6 +1,7 @@ const { expect } = require( "chai" ); const request = require( "supertest" ); const _ = require( "lodash" ); +const EC = require( "../lib/elastic_config" ); const Mapper = require( "../lib/elastic_mapper" ); const helpers = require( "./lib/helpers" ); @@ -202,9 +203,8 @@ describe( "ElasticMapper", ( ) => { describe( "defaults", ( ) => { it( "creates a default config", ( ) => { app = Mapper.server( ); - expect( global.config.environment ).to.eql( "development" ); - expect( global.config.tileSize ).to.eql( 256 ); - expect( global.config.debug ).to.eql( true ); + expect( EC.config.tileSize ).to.eql( 256 ); + expect( EC.config.debug ).to.eql( true ); } ); } ); } ); diff --git a/test/lib/helpers.js b/test/lib/helpers.js index bb10dfa..bef6b19 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -1,4 +1,5 @@ const ElasticRequest = require( "../../lib/elastic_request" ); +const EC = require( "../../lib/elastic_config" ); const helpers = { }; @@ -8,7 +9,7 @@ helpers.testConfig = ( ) => ( helpers.rebuildTestIndex = async ( ) => { ElasticRequest.createClient( ); - const indexOptions = { index: global.config.elasticsearch.searchIndex }; + const indexOptions = { index: EC.config.elasticsearch.searchIndex }; if ( await ElasticRequest.esClient.indices.exists( indexOptions ) ) { await ElasticRequest.esClient.indices.delete( indexOptions ); await helpers.createTestIndex( ); @@ -35,7 +36,7 @@ helpers.createTestIndex = async ( ) => { } } }; - const indexOptions = { index: global.config.elasticsearch.searchIndex }; + const indexOptions = { index: EC.config.elasticsearch.searchIndex }; await ElasticRequest.esClient.indices.create( indexOptions ); await ElasticRequest.esClient.indices.putMapping( { ...indexOptions, @@ -59,7 +60,7 @@ helpers.createTestIndex = async ( ) => { helpers.deleteTestIndex = async ( ) => { ElasticRequest.createClient( ); - const indexOptions = { index: global.config.elasticsearch.searchIndex }; + const indexOptions = { index: EC.config.elasticsearch.searchIndex }; if ( await ElasticRequest.esClient.indices.exists( indexOptions ) ) { await ElasticRequest.esClient.indices.delete( indexOptions ); }