index.js (1046B)
1 2 3 export default { 4 async fetch(request) { 5 const urls = parseUrlsFromEnv(); 6 const now = new Date(); 7 let output = ""; 8 9 for (const url of urls) { 10 output += `<tr><td>${url}</td><td>${await(responseIsOk(url))}</td></tr>` 11 } 12 13 const html = `<!DOCTYPE html> 14 <body> 15 <h1>STATUS</h1> 16 <table> 17 <tr> 18 <td>Source</td> 19 <td>Status</td> 20 </tr> 21 ${output} 22 </table> 23 <p>Last updated: ${now.toISOString()}</p> 24 </body>`; 25 26 return new Response(html, { 27 headers: { 28 "content-type": "text/html;charset=UTF-8", 29 }, 30 }); 31 }, 32 }; 33 34 function parseUrlsFromEnv() { 35 const urls = process.env.URLS || ""; 36 return urls.split(","); 37 } 38 39 async function responseIsOk(url) { 40 try { 41 const response = await fetch(url, { redirect: "follow" }); 42 if (response.status < 500) { 43 return "<span style='color:green;'>online</span>"; 44 } 45 return "<span style='color:red;'>offline</span>"; 46 } catch { 47 return "<span style='color:red;'>offline</span>"; 48 } 49 }