Skip to content

Commit

Permalink
Switch geoIP service from Telize.com to freegeoip.net
Browse files Browse the repository at this point in the history
  • Loading branch information
JarnoLeConte committed Nov 19, 2015
1 parent fa45c00 commit 02641ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
21 changes: 12 additions & 9 deletions packages/util-geo/geo.jsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ Geo = {}
Geo.geocode = function(address, cb) {
var url = "http://api.tiles.mapbox.com/v4/geocode/mapbox.places-v1/"+address+".json";
var options = {params: { format: "json", access_token: Settings['mapbox'].token }};

var response = function(res) {
var data = res && res.content && JSON.parse(res.content);
var feature = data && data.features && data.features[0];
var center = feature && feature.center;
if (center)
return { lat: center[1], lng: center[0] };
}

if (Meteor.isServer)
return response(HTTP.get(url, options));
else
Expand Down Expand Up @@ -73,13 +73,13 @@ Geo.getDistanceFromLatLonObj = function(latlong1, latlong2) {
Geo.getDistanceFromLatLon = function(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = Geo.deg2rad(lat2-lat1); // deg2rad below
var dLon = Geo.deg2rad(lon2-lon1);
var a =
var dLon = Geo.deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Geo.deg2rad(lat1)) * Math.cos(Geo.deg2rad(lat2)) *
Math.cos(Geo.deg2rad(lat1)) * Math.cos(Geo.deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d; // km
}
Expand All @@ -104,10 +104,13 @@ Geo.deg2rad = function(deg) {
* ```
*/
Geo.requestLocationForIp = function(ip) {
if (ip === "127.0.0.1")
if (ip === "127.0.0.1")
ip = ""; // if on localhost, let telize determine my ip.
try {
var data = HTTP.get("http://www.telize.com/geoip/" + ip, {timeout: 2000}).data;
var url = "http://www.telize.com/geoip/" + ip; // service discontinued since 15th november 2015
url = "http://freegeoip.net/json/" + ip;

var data = HTTP.get(url, {timeout: 2000}).data;
return _.pick(data, 'longitude', 'latitude');
} catch(err) {
console.log("request client-IP error:", err);
Expand Down
22 changes: 11 additions & 11 deletions server/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Router.route('/mandrill-webhook', function() {
var events = EJSON.parse(Object.property(this.request.body, 'mandrill_events') || "[]");

var addEvent = function(event) {
// event format: http://help.mandrill.com/entries/24466132-Webhook-Format
if (!event.event)
// event format: http://help.mandrill.com/entries/24466132-Webhook-Format

if (!event.event)
return; /* not a message event */

console.log('Email webhook event:', event.event);
Expand All @@ -22,7 +22,7 @@ Router.route('/mandrill-webhook', function() {
client: event.user_agent_parsed.ua_family,
version: event.user_agent_parsed.ua_version,
};

// link event to original message
EmailsOutbound.update({'to.messageId': messageId}, {
$push: { 'to.$.events': {
Expand All @@ -49,26 +49,26 @@ Router.route('/mandrill-webhook', function() {


Router.route('/:catchAll?', function () {
var url = getRequestUrl(this.request);
var url = getRequestUrl(this.request);
var city = Url.city(url);
var isLocalhost = Url.isLocalhost(url);

// check if there is a valid city present in the url
if (!city || !City.lookup(city)) {

// this subdomain doesn't exist or isn't a valid city

// we try to find the closest city
var userIp = getClientIp(this.request);
var location = Geo.requestLocationForIp(userIp);
var closestCity = Geo.findClosestCity(location);
var closestCity = Geo.findClosestCity(location);

if (closestCity) {

// if closest city is found we redirect the user to a new url
var cityUrl = Url.replaceCity(closestCity, url);
return redirect(cityUrl, this.response);

} else if (city !== 'www') {

// If closest city can't be found, we redirect to www.
Expand All @@ -77,7 +77,7 @@ Router.route('/:catchAll?', function () {
return redirect(cityUrl, this.response);
}
}

// done!
this.next();

Expand Down

0 comments on commit 02641ba

Please sign in to comment.