generated from nhcarrigan/template
feat: add health check server
This commit is contained in:
parent
9def06d149
commit
6bfe251fe7
@ -12,7 +12,7 @@
|
||||
},
|
||||
"engines": {
|
||||
"node": "20",
|
||||
"pnpm": "8"
|
||||
"pnpm": "9"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -43,6 +43,7 @@
|
||||
"@sentry/node": "7.114.0",
|
||||
"discord.js": "14.15.2",
|
||||
"dotenv": "16.4.5",
|
||||
"fastify": "5.0.0",
|
||||
"winston": "3.13.0"
|
||||
}
|
||||
}
|
||||
|
2984
pnpm-lock.yaml
generated
2984
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -3,6 +3,7 @@ import * as Sentry from "@sentry/node";
|
||||
import { Client, GatewayIntentBits, WebhookClient } from "discord.js";
|
||||
|
||||
import { manageRoles } from "./modules/manageRoles";
|
||||
import { instantiateServer } from "./server/serve";
|
||||
import { errorHandler } from "./utils/errorHandler";
|
||||
|
||||
Sentry.init({
|
||||
@ -43,6 +44,7 @@ Sentry.init({
|
||||
});
|
||||
|
||||
await bot.login(process.env.DISCORD_TOKEN);
|
||||
await instantiateServer(bot);
|
||||
} catch (err) {
|
||||
await errorHandler("index", err);
|
||||
}
|
||||
|
48
src/server/serve.ts
Normal file
48
src/server/serve.ts
Normal 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);
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user