Skip to content

Instantly share code, notes, and snippets.

@Marak
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Marak/2585b2556386ffe1c31a to your computer and use it in GitHub Desktop.
Save Marak/2585b2556386ffe1c31a to your computer and use it in GitHub Desktop.
hook.io microservice for geoip
module['exports'] = function geoipHook (hook) {
var geoip = require('geoip-lite');
var ip = hook.params.ip;
if (typeof ip === "undefined" || ip.length === 0) {
ip = hook.req.headers['x-forwarded-for'];
}
console.log('attempting to lookup ' + ip);
var geo = geoip.lookup(ip);
if (geo === null) {
return hook.res.end(JSON.stringify({ message: "invalid ip " + ip.toString(), error: true }, true, 2));
}
geo.ip = ip;
return hook.res.end(JSON.stringify(geo, true, 2));
};
module['exports'].schema = {
"ip": {
"type": "string"
}
};
module['exports'].view = "https://gist.githubusercontent.com/Marak/2585b2556386ffe1c31a/raw/view.html";
module['exports'].presenter = "https://gist.githubusercontent.com/Marak/2585b2556386ffe1c31a/raw/presenter.js";
module['exports'] = function presentGeoIpHook(opts, cb) {
var $ = this.$;
var geo = JSON.parse(opts.output.toString());
if (geo.error) {
} else {
$('.lat').attr('value', geo.ll[0].toString());
$('.lng').attr('value', geo.ll[1].toString());
$('.ip').attr('value', geo.ip);
}
cb(null, $.html());
};
<!DOCTYPE html>
<html>
<head>
<title>Lat/Lng Object Literal</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta charset="utf-8"/>
<style>
html, body {
height: 100%;
margin: 0px;
padding: 0px
}
h1, h2, h3 {
margin: 0px;
padding: 0px;
}
#map-canvas {
height: 80%;
width: 98%;
}
input {
font-size: 24px;
}
.container {
text-align: center;
}
</style>
<script type="text/javascript" src="https://hook.io/js/jquery.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
// In this example, we center the map, and add a marker, using a LatLng object
// literal instead of a google.maps.LatLng object. LatLng object literals are
// a convenient way to add a LatLng coordinate and, in most cases, can be used
// in place of a google.maps.LatLng object.
$(document).ready(function(){
var map;
var lat = Number($('.lat').val()), lng = Number($('.lng').val());
function initialize() {
var mapOptions = {
zoom: 8,
center: {lat: lat, lng: lng}
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var marker = new google.maps.Marker({
// The below line is equivalent to writing:
// position: new google.maps.LatLng(-34.397, 150.644)
position: {lat: lat, lng: lng},
map: map
});
map.setMapTypeId('hybrid')
// You can use a LatLng literal in place of a google.maps.LatLng object when
// creating the Marker object. Once the Marker object is instantiated, its
// position will be available as a google.maps.LatLng object. In this case,
// we retrieve the marker's position using the
// google.maps.LatLng.getPosition() method.
var infowindow = new google.maps.InfoWindow({
content: '<p>Marker Location:' + marker.getPosition() + '</p>'
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
});
</script>
</head>
<body align="center">
<h1>GeoIP lookup</h1>
<h2>Specify and IP address, find it's physical address</h2>
<div class="container">
<form action="" method="POST">
<input type="text" class="ip" name="ip" id="ip" value=""/>
<input type="hidden" class="lat" name="lat" value=""/>
<input type="hidden" class="lng" name="lng" value=""/>
<input type="submit" value="Search IP"/>
</form>
</div>
<div id="map-canvas" align="center"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment