Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to avoid global.config #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/elastic_config.js
Original file line number Diff line number Diff line change
@@ -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
}
};
37 changes: 12 additions & 25 deletions lib/elastic_mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" );

Expand Down Expand Up @@ -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 )
: [];
Expand Down Expand Up @@ -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 );
Expand All @@ -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 ) {
Expand All @@ -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;
};
Expand Down
29 changes: 15 additions & 14 deletions lib/elastic_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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 );
}
Expand All @@ -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
Expand Down Expand Up @@ -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
} );
Expand Down Expand Up @@ -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]],
Expand Down Expand Up @@ -214,7 +215,7 @@ ElasticRequest.geotileGridPrecision = ( zoom, offset ) => {
};

ElasticRequest.defaultMapFields = ( ) => (
["id", global.config.elasticsearch.geoPointField]
["id", EC.config.elasticsearch.geoPointField]
);

ElasticRequest.defaultMapQuery = ( ) => (
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions lib/map_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } );
}
};

Expand All @@ -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,
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions test/elastic_mapper.js
Original file line number Diff line number Diff line change
@@ -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" );

Expand Down Expand Up @@ -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 );
} );
} );
} );
7 changes: 4 additions & 3 deletions test/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ElasticRequest = require( "../../lib/elastic_request" );
const EC = require( "../../lib/elastic_config" );

const helpers = { };

Expand All @@ -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( );
Expand All @@ -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,
Expand All @@ -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 );
}
Expand Down