Last active
December 12, 2017 08:42
-
-
Save tsertkov/e9060519fd5eaf5ec439dcec71be5001 to your computer and use it in GitHub Desktop.
JS function using aws-sdk to download json map from s3 and use it to map request uri to redirect url
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
const AWS = require('aws-sdk') | |
const s3 = new AWS.S3() | |
function fetchJson (bucket, key) { | |
return new Promise((resolve, reject) => { | |
s3.getObject({ Bucket: bucket, Key: key }, (err, data) => { | |
if (err) return reject(err) | |
try { | |
const bodyString = data.Body.toString('utf8') | |
const obj = JSON.parse(bodyString) | |
resolve(obj) | |
} catch (e) { | |
reject(err) | |
} | |
}) | |
}) | |
} | |
function mapRequestToRedirect (requestUri, redirectMap) { | |
// FIXME implement mapping logic here | |
return '_default_url_' | |
} | |
function getRedirectUrl (requestUri, bucket, key) { | |
return new Promise((resolve, reject) => { | |
fetchJson(bucket, key) | |
.then(redirectMap => { | |
const redirectUrl = mapRequestToRedirect(requestUri, redirectMap) | |
resolve(redirectUrl) | |
}) | |
}) | |
} | |
const requestUri = '/go/some-ico' | |
const bucket = 'example.com' | |
const key = 'redirect-map.json' | |
getRedirectUrl(requestUri, bucket, key) | |
.then(redirectUrl => { | |
console.log(`Redirect to: ${redirectUrl}`) | |
}) | |
.catch(err => { | |
console.error(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment