feat: add health check server

This commit is contained in:
2024-09-26 11:17:31 -07:00
parent 9def06d149
commit 6bfe251fe7
4 changed files with 1881 additions and 1156 deletions

48
src/server/serve.ts Normal file
View File

@ -0,0 +1,48 @@
import { readFile } from "fs/promises";
import { type Client, WebhookClient } from "discord.js";
import fastify from "fastify";
import { errorHandler } from "../utils/errorHandler";
/**
* Starts up a web server for health monitoring.
*
* @param {Client} bot The bot's Discord instance.
*/
export const instantiateServer = async (bot: Client) => {
try {
const server = fastify({
logger: false,
https: {
cert: await readFile(
"/etc/letsencrypt/live/oogie.nhcarrigan.com-0001/cert.pem"
),
key: await readFile(
"/etc/letsencrypt/live/oogie.nhcarrigan.com-0001/privkey.pem"
),
},
});
server.get("/", (_req, res) => {
res.send("Health check~!");
});
server.listen({ port: 1443 }, (err) => {
if (err) {
void errorHandler("start server", err);
return;
}
const hook = new WebhookClient({ url: process.env.DEBUG_HOOK as string });
void hook.send({
avatarURL:
bot.user?.displayAvatarURL() ??
"https://cdn.nhcarrigan.com/profile.png",
content: "Fastify server live on port 1443~!",
username: bot.user?.username ?? "Hacktoberfest",
});
});
} catch (err) {
await errorHandler("instantiate server", err);
}
};