From 77621acb2d7201646bebb0156396818c415e8344 Mon Sep 17 00:00:00 2001 From: Hector Alfaro Date: Wed, 30 Oct 2019 14:23:59 -0400 Subject: [PATCH] add lambda fcn handler --- handler.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 handler.js diff --git a/handler.js b/handler.js new file mode 100644 index 0000000..d1ac2c1 --- /dev/null +++ b/handler.js @@ -0,0 +1,41 @@ +const fs = require('fs') +const path = require('path') + +const files = { + '/public/index.css': { + content: fs.readFileSync(path.join(__dirname, 'public', 'index.css'), 'utf8'), + type: 'text/css' + }, + '/public/main.js': { + content: fs.readFileSync(path.join(__dirname, 'public', 'main.js'), 'utf8'), + type: 'text/javascript' + }, + '/': { + content: fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8'), + type: 'text/html' + } +} + +/** + * + * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + * @param {Object} event - API Gateway Lambda Proxy Input Format + * + * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html + * @param {Object} context + * + * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + * @returns {Object} object - API Gateway Lambda Proxy Output Format + * + */ +exports.lambdaHandler = async (event, context) => { + // This will either be /, /public/index.css, or /public/main.js + const requestPath = event.path + const { content, type } = files[requestPath] + + return { + headers: { 'content-type': type }, + statusCode: 200, + body: content + } +}