Browse Source

Functional

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

33
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" ]

56
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;

3
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",

25
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
};
Loading…
Cancel
Save