2021-03-24 08:28:00 +03:00
|
|
|
const path = require("path");
|
2021-03-24 05:34:12 +03:00
|
|
|
|
2021-03-25 18:04:59 +03:00
|
|
|
exports.handler =
|
|
|
|
/**
|
|
|
|
* @param {import('aws-lambda').APIGatewayProxyEvent} event
|
|
|
|
* @param {any} context
|
|
|
|
*/
|
|
|
|
async function (event, context) {
|
|
|
|
console.log(JSON.stringify(event));
|
2021-04-25 02:01:31 +03:00
|
|
|
global.staticHttpCache = {};
|
2021-03-24 08:28:00 +03:00
|
|
|
|
2021-03-25 18:04:59 +03:00
|
|
|
const compiledElmPath = path.join(__dirname, "elm-pages-cli.js");
|
|
|
|
const renderer = require("../../../../generator/src/render");
|
2021-03-29 05:30:38 +03:00
|
|
|
try {
|
2021-04-25 02:01:31 +03:00
|
|
|
const renderResult = await renderer(
|
|
|
|
compiledElmPath,
|
|
|
|
event.path,
|
|
|
|
event,
|
|
|
|
function () {}
|
|
|
|
);
|
2021-03-29 05:30:38 +03:00
|
|
|
|
2021-03-29 18:59:53 +03:00
|
|
|
if (renderResult.kind === "json") {
|
|
|
|
return {
|
|
|
|
body: renderResult.contentJson,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"x-powered-by": "elm-pages",
|
|
|
|
},
|
|
|
|
statusCode: 200,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
body: renderResult.htmlString,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
"x-powered-by": "elm-pages",
|
|
|
|
},
|
|
|
|
statusCode: 200,
|
|
|
|
};
|
|
|
|
}
|
2021-03-29 05:30:38 +03:00
|
|
|
} catch (error) {
|
2021-04-25 02:01:31 +03:00
|
|
|
console.error(error);
|
2021-03-29 05:30:38 +03:00
|
|
|
return {
|
|
|
|
body: `<body><h1>Error</h1><pre>${error}</pre></body>`,
|
|
|
|
statusCode: 500,
|
2021-03-29 18:59:53 +03:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
"x-powered-by": "elm-pages",
|
|
|
|
},
|
2021-03-29 05:30:38 +03:00
|
|
|
};
|
|
|
|
}
|
2021-03-24 05:34:12 +03:00
|
|
|
};
|