status-page/src/index.ts
2023-10-28 18:28:50 +02:00

46 lines
No EOL
1.7 KiB
TypeScript

import {database} from "./db.ts";
import Elysia from "elysia";
import Handlebars from "handlebars";
import {readFile} from "fs/promises";
import {LISTEN_PORT} from "./consts.ts";
import {html} from "@elysiajs/html";
import staticPlugin from "@elysiajs/static";
import {hash} from "bun";
Handlebars.registerHelper("status", (status, options) => {
return options.fn({
status,
...options.data.root["statuses"][status]
})
})
Handlebars.registerHelper("inlineStatus", (statusID, options) => {
let status = options.data.root["statuses"][statusID]
return new Handlebars.SafeString(`<a href="#status-${statusID}" style="color: ${status.color}" class="inline-status status-${statusID}">${status.displayName}</a>`)
})
Handlebars.registerHelper("inlineService", (serviceID, options) => {
let service = options.data.root["services"][serviceID]
return new Handlebars.SafeString(`<span class="inline-service service-${serviceID}">${service.displayName}</span>`)
})
Handlebars.registerHelper("epochUTC", (time) => {
return new Handlebars.SafeString(`<time datetime="${(new Date(time)).toISOString()}">UTC: ${(new Date(time)).toLocaleString()}</time>`)
})
Handlebars.registerHelper("gravatar", (email) => {
let hasher = new Bun.CryptoHasher("sha256")
hasher.update(email.trim().toLowerCase())
let src = "https://gravatar.com/avatar/" + hasher.digest("hex")
return new Handlebars.SafeString(`<img height="40px" src=${src}>`)
})
let template = Handlebars.compile((await readFile("templates/index.hbs")).toString())
new Elysia()
.use(html())
.use(staticPlugin())
.get("/", () => template(database.data))
.listen(LISTEN_PORT)
console.log(`Status Page is running on port ${LISTEN_PORT}`)