Last active
May 23, 2018 15:14
-
-
Save alfonsogoberjr/147ff4130d6f9f9cb52856f13e37b08d to your computer and use it in GitHub Desktop.
serverless-geolocation service - handler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const util = require("util"); | |
const Cookie = require("cookie"); | |
const Reader = require("mmdb-reader"); | |
const DOMAIN = "yourdomain.com"; # replace this with your domain name | |
// edgeRedirect solves the Cloudfront website use-case | |
/* | |
Typical Cloudfront event structure to consume: | |
{ | |
"Records": [ | |
{ | |
"cf": { | |
"request": { | |
"uri": "/test", | |
"headers": { | |
"cloudfront-viewer-country": [ | |
{ | |
key: "Cloudfromt-Viewer-Country", | |
value: "FR" | |
} | |
] | |
} | |
} | |
} | |
} | |
] | |
} | |
*/ | |
module.exports.edgeRedirect = (event, context, callback) => { | |
const request = event.Records[0].cf.request; | |
const headers = request.headers; | |
const uri = request.uri === "/index.html" ? "/" : request.uri; | |
const countryCode = headers["cloudfront-viewer-country"][0].value; | |
console.log(`Country detected: '${countryCode}'`); | |
if (countryCode !== "US") { | |
// Non-US IP was detected by CloudFront | |
const response = { | |
status: "302", | |
statusDescription: "Found", | |
headers: { | |
location: [ | |
{ | |
key: "Location", | |
value: `https://${countryCode.toLowerCase()}.${DOMAIN}${uri}` | |
} | |
], | |
["cache-control"]: [ | |
{ | |
key: "Cache-Control", | |
value: "no-cache, no-store, private" | |
} | |
] | |
} | |
}; | |
callback(null, response); | |
} else { | |
// Allow request to continue as intended | |
callback(null, request); | |
} | |
}; | |
// countryLookup solves all non-CloudFront use-cases, such as mobile | |
module.exports.countryLookup = ( | |
{ requestContext }, | |
ctx, | |
callback | |
) => { | |
let ip = | |
requestContext && | |
requestContext.identity && | |
requestContext.identity.sourceIp | |
? requestContext.identity.sourceIp | |
: callback(null, { statusCode: 422, body: "Unprocessable Request" }); | |
Reader.open(__dirname + "/data/GeoLite2-Country.mmdb", (error, reader) => { | |
if (error) callback(error); | |
else { | |
let { iso_code, is_in_european_union } = reader.lookup(ip).country; | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify({ | |
iso_code, | |
is_in_european_union: is_in_european_union || false | |
}) | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment