Browse Source

Functional

pull/1076/head
Gibran Parvez 7 years ago
parent
commit
ecbaf2aa8c
  1. 33
      Dockerfile
  2. 42
      controller/convert.js
  3. 3
      package.json
  4. 23
      service/external.js

33
Dockerfile

@ -5,7 +5,6 @@ FROM pelias/libpostal_baseimage
LABEL maintainer="pelias@mapzen.com" LABEL maintainer="pelias@mapzen.com"
EXPOSE 3100 EXPOSE 3100
EXPOSE 3150
# Where the app is built and run inside the docker fs # Where the app is built and run inside the docker fs
ENV WORK=/opt/pelias ENV WORK=/opt/pelias
@ -13,27 +12,31 @@ ENV WORK=/opt/pelias
# Used indirectly for saving npm logs etc. # Used indirectly for saving npm logs etc.
ENV HOME=/opt/pelias ENV HOME=/opt/pelias
# Update git submodules #Set geotrans IP
RUN git submodule update --init --recursive --remote ENV GEOTRANS_IP=10.0.2.62
WORKDIR ${WORK} WORKDIR ${WORK}
COPY . ${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 # Build and set permissions for arbitrary non-root user
RUN npm install && \ RUN npm install && \
npm test && \ npm test && \
chmod -R a+rwX . 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 geotrans env variable script
#RUN . ./node_modules/geotrans-mgrs-converter/install.sh #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). # 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} RUN chown -R 9999:9999 ${WORK}
# USER 9999 # USER 9999
# start service
CMD [ "npm", "start" ]

42
controller/convert.js

@ -1,43 +1,37 @@
"use strict"; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const DATUM = 'WGE'; const DATUM = 'WGE';
const external = require('../service/external'); const external = require('../service/external');
const logger = require( 'pelias-logger' ).get( 'api' );
"use strict";
function converter( req, res, next) { function converter( req, res, next) {
let result;
try{
if(_.find(req.query, (val, key) => val === 'mgrs')){ 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'){ if(req.query.from === 'mgrs' && req.query.to === 'decdeg'){
logger.info("testing"); try{
result = external.geotrans(req.query.q); external.geotrans(req.query.q).then(function(gtResult){
logger.info(result); if(typeof gtResult === 'string' && gtResult.indexOf('ERROR') > -1){
res.send(gtResult);
throw gtResult;
} }
else{
res.send(addQueryProperties(gtResult, req.query));
} }
if(typeof result === 'string' && result.indexOf('ERROR') > -1){ }).catch(function(error){
//Relay error res.send({'error':error});
throw result; });
} }
result.properties.from = req.query.from; catch(error){
result.properties.to = req.query.to; res.send({'error': error});
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; module.exports = converter;

3
package.json

@ -65,7 +65,8 @@
"predicates": "^2.0.0", "predicates": "^2.0.0",
"retry": "^0.10.1", "retry": "^0.10.1",
"stats-lite": "^2.0.4", "stats-lite": "^2.0.4",
"through2": "^2.0.3" "through2": "^2.0.3",
"axios": "^0.17.1"
}, },
"devDependencies": { "devDependencies": {
"ciao": "^1.0.0", "ciao": "^1.0.0",

23
service/external.js

@ -1,16 +1,21 @@
const request = require('request');
const logger = require('pelias-logger').get('api');
"use strict"; "use strict";
async function geotrans(coord) { const axios = require('axios');
const geotransIP = process.env.GEOTRANS_IP;
function geotrans(coord) {
let result; let result;
request(`http://10.0.2.62:3150?datum=WGE&coord=${coord}`, function (error, response, body) { return axios.get(`http://${geotransIP}:3150`, {
logger.info(response.body); params:{
result = response.body; 'datum':'WGE',
'coord':coord
}
})
.then(function (response){
return response.data;
}).catch(function (reason){
return reason;
}); });
return result;
} }
module.exports = { module.exports = {

Loading…
Cancel
Save