Skip to content

Instantly share code, notes, and snippets.

@lbogdan
Created December 20, 2017 19:35
Show Gist options
  • Save lbogdan/97ed2dc5954b8b2e8057c5d9ea1bd448 to your computer and use it in GitHub Desktop.
Save lbogdan/97ed2dc5954b8b2e8057c5d9ea1bd448 to your computer and use it in GitHub Desktop.
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