import {corsOptions} from "./corsHandler";
const handlePages=async (request, env, ctx, trigger='404') => {
switch (true) {
case "200": {
return new Response(JSON.stringify({res: "Hi"}), {status: 200})
}
default :
return await handle404Pages(request, env, ctx)
}
}
const returnPage=async (htmlResponse, request, status=200) => {
switch (request.method) {
case 'GET':
return new Response(htmlResponse, {
status: status,
headers: {
'Content-Type': 'text/html'
}
});
default:
const responseData={
"status": "Not Found"
}
const jsonResponse=JSON.stringify(responseData)
// Return JSON response with appropriate CORS headers
return new Response(jsonResponse, corsOptions(request), 404);
}
}
const getPageR2=async (env, page) => {
return await env.STORE.get(`src/html/${page}.html`)
}
async function handle404Pages(request, env, ctx) {
const ContactMeObject=JSON.stringify(env.CONTACTME);
console.log(ContactMeObject)
const object=await getPageR2(env, "404")
if (object === null) {
return new Response('Object Not Found', {status: 404});
}
console.log(JSON.stringify(object))
const htmlContent=object
.replace('{{URL}}', ContactMeObject['WHERE'])
.replace('{{CONTACT}}', ContactMeObject[ContactMeObject['WHERE']]);
return await returnPage(htmlContent, request, 404)
}
export {handlePages}