Created
December 20, 2017 19:35
-
-
Save lbogdan/97ed2dc5954b8b2e8057c5d9ea1bd448 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Koa = require('koa'); | |
const Router = require('koa-router'); | |
const AWS = require('aws-sdk'); | |
const app = new Koa(); | |
const router = new Router(); | |
const thumbnails = {}; | |
AWS.config.loadFromPath('./aws_lambda.json'); | |
const lambda = new AWS.Lambda({ | |
region: 'eu-central-1', | |
apiVersion: '2015-03-31', | |
}); | |
async function getThumbnailUrl(sandboxId) { | |
const pullParams = { | |
FunctionName: 'codesandbox-thumbnails-lambda', | |
InvocationType: 'RequestResponse', | |
LogType: 'None', | |
Payload: JSON.stringify({ sandboxId }), | |
}; | |
const response = await lambda.invoke(pullParams).promise(); | |
const payload = JSON.parse(response.Payload); | |
return payload.Location; | |
} | |
router.get('/:sandboxId.png', async ctx => { | |
const { sandboxId } = ctx.params; | |
if (!(sandboxId in thumbnails)) { | |
thumbnails[sandboxId] = await getThumbnailUrl(sandboxId); | |
} | |
ctx.redirect(thumbnails[sandboxId]); | |
}); | |
app | |
.use(router.routes()) | |
.use(router.allowedMethods()) | |
.listen(9000); | |
console.log('listening on http://localhost:9000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment