diff --git a/Dockerfile b/Dockerfile index 5b12735f..d2ebaede 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,6 @@ FROM pelias/libpostal_baseimage LABEL maintainer="pelias@mapzen.com" EXPOSE 3100 -EXPOSE 3150 # Where the app is built and run inside the docker fs ENV WORK=/opt/pelias @@ -13,27 +12,31 @@ ENV WORK=/opt/pelias # Used indirectly for saving npm logs etc. ENV HOME=/opt/pelias -# Update git submodules -RUN git submodule update --init --recursive --remote +#Set geotrans IP +ENV GEOTRANS_IP=10.0.2.62 WORKDIR ${WORK} COPY . ${WORK} +# install required utilities +RUN apt-get update && \ + apt-get install -y vim curl + +# install node 6.x +RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \ + apt-get install -y nodejs + +# move original node and symlink +RUN mv /usr/local/bin/node /usr/local/bin/node.original + +RUN ln -s /usr/bin/nodejs /usr/local/bin/node + # Build and set permissions for arbitrary non-root user RUN npm install && \ npm test && \ chmod -R a+rwX . - - -# Compile GeoTrans -RUN npm install --unsafe-perm geotrans-mgrs-converter -# Install node-gyp for GeoTrans -RUN npm install -g node-gyp -# Configure and build NBIND for Geotrans node module -RUN node-gyp configure build --directory=node_modules/geotrans-mgrs-converter/ - # Run geotrans env variable script #RUN . ./node_modules/geotrans-mgrs-converter/install.sh # Don't run as root, because there's no reason to (https://docs.docker.com/engine/articles/dockerfile_best-practices/#user). @@ -41,3 +44,9 @@ RUN node-gyp configure build --directory=node_modules/geotrans-mgrs-converter/ RUN chown -R 9999:9999 ${WORK} # USER 9999 +# start service +CMD [ "npm", "start" ] + + + + diff --git a/controller/convert.js b/controller/convert.js index 21cd08a9..dcffd5b0 100644 --- a/controller/convert.js +++ b/controller/convert.js @@ -1,43 +1,37 @@ -"use strict"; - +'use strict'; const _ = require('lodash'); const DATUM = 'WGE'; const external = require('../service/external'); -const logger = require( 'pelias-logger' ).get( 'api' ); - -"use strict"; - function converter( req, res, next) { - let result; - try{ if(_.find(req.query, (val, key) => val === 'mgrs')){ - //If mgrs is specified as a conversion parameter - //let mgrsConverter = new MGRS_converter(DATUM); - if(req.query.from === 'mgrs' && req.query.to === 'decdeg'){ - logger.info("testing"); - result = external.geotrans(req.query.q); - logger.info(result); - } - } - if(typeof result === 'string' && result.indexOf('ERROR') > -1){ - //Relay error - throw result; + if(req.query.from === 'mgrs' && req.query.to === 'decdeg'){ + try{ + external.geotrans(req.query.q).then(function(gtResult){ + if(typeof gtResult === 'string' && gtResult.indexOf('ERROR') > -1){ + res.send(gtResult); + throw gtResult; + } + else{ + res.send(addQueryProperties(gtResult, req.query)); + } + }).catch(function(error){ + res.send({'error':error}); + }); + } + catch(error){ + res.send({'error': error}); + } + } } - result.properties.from = req.query.from; - result.properties.to = req.query.to; - if(result.properties.name.toLowerCase() !== req.query.q.toLowerCase()){ - result.properties.name = req.query.q.toUpperCase(); - } - } - catch(error){ - result = {"error": error}; - } - finally{ - res.send(result); - } +} + +function addQueryProperties (geojson, query){ + geojson.properties.from = query.from; + geojson.properties.to = query.to; + return geojson; } module.exports = converter; diff --git a/package.json b/package.json index d32e37cc..1fde4123 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,8 @@ "predicates": "^2.0.0", "retry": "^0.10.1", "stats-lite": "^2.0.4", - "through2": "^2.0.3" + "through2": "^2.0.3", + "axios": "^0.17.1" }, "devDependencies": { "ciao": "^1.0.0", diff --git a/service/external.js b/service/external.js index caf1ab74..c341852f 100644 --- a/service/external.js +++ b/service/external.js @@ -1,18 +1,23 @@ -const request = require('request'); -const logger = require('pelias-logger').get('api'); - "use strict"; -async function geotrans(coord) { +const axios = require('axios'); +const geotransIP = process.env.GEOTRANS_IP; + +function geotrans(coord) { let result; - request(`http://10.0.2.62:3150?datum=WGE&coord=${coord}`, function (error, response, body) { - logger.info(response.body); - result = response.body; + return axios.get(`http://${geotransIP}:3150`, { + params:{ + 'datum':'WGE', + 'coord':coord + } + }) + .then(function (response){ + return response.data; + }).catch(function (reason){ + return reason; }); - return result; - } - + module.exports = { geotrans: geotrans }; \ No newline at end of file