Skip to content

Instantly share code, notes, and snippets.

@faultables
Last active October 16, 2024 08:17
Show Gist options
  • Save faultables/4795a5a18d46a90ffe44084ac5157841 to your computer and use it in GitHub Desktop.
Save faultables/4795a5a18d46a90ffe44084ac5157841 to your computer and use it in GitHub Desktop.
cf worker + uptime kuma
<script>
document.addEventListener("DOMContentLoaded", function() {
const cfWorkerURL = "https://status.edgy0.workers.dev/"
const kumaURL = "https://status.faultables.net"
const homelabTitle = "faultables homelab"
fetch(cfWorkerURL)
.then(res => res.json())
.then(payload => {
const data = payload.data
if (data.downCount > 0) {
const container = document.getElementById("status")
const learnMore = document.createElement("a")
learnMore.href = kumaURL
learnMore.rel = "noopener noreferer"
learnMore.target = "_blank"
learnMore.innerText = "learn more here"
const text = document.createElement("p")
text.innerText = `${data.downCount} out of ${data.servicesCount} services from ${homelabTitle} are down — `
container.style.display = "block"
text.appendChild(learnMore)
container.appendChild(text)
}
})
})
</script>
export default {
async fetch(request, env, ctx) {
const kumaURL = "https://status.faultables.net/"
const url = kumaURL + "api/status-page/heartbeat/home";
async function gatherResponse(response) {
const payload = await response.json()
const data = Object.values(payload.heartbeatList).flatMap(items => items.slice(-1))
return {
_cloudflareCacheStatus: response.headers.get("cf-cache-status"),
servicesCount: data.length || 0,
downCount: data.filter(item => item.status === 0).length || 0
}
}
const res = await fetch(url, {
cf: {
cacheTtl: 30,
cacheEverything: true
},
})
const result = await gatherResponse(res);
const options = {
headers: { "content-type": "application/json" }
}
const response = new Response(JSON.stringify({ data: result }), options);
response.headers.set('Access-Control-Allow-Origin', '*');
response.headers.set('Access-Control-Max-Age', '86400');
response.headers.set("Cache-Control", "max-age=30");
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment