Add healthcheck endpoint

This commit is contained in:
Christopher Cookman 2024-11-11 07:04:41 -07:00
parent fdc05cd165
commit a32f3e09c0
3 changed files with 63 additions and 33 deletions

1
.gitignore vendored
View file

@ -133,3 +133,4 @@ dist
database.db database.db
tts/* tts/*
!tts/.gitkeep !tts/.gitkeep
tts.json

View file

@ -188,7 +188,7 @@ function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber
newCooldown = new Date(); newCooldown = new Date();
newCooldown.setMinutes(newCooldown.getMinutes() + 5); newCooldown.setMinutes(newCooldown.getMinutes() + 5);
db.run("UPDATE accounts SET cooldown = ? WHERE id = ?", newCooldown.toISOString() ,accountNumber, (err) => { db.run("UPDATE accounts SET cooldown = ? WHERE id = ?", newCooldown.toISOString(), accountNumber, (err) => {
if (err) { if (err) {
console.error(err); console.error(err);
} }
@ -326,7 +326,7 @@ function sendVerificationCode(account) {
newCooldown = new Date(); newCooldown = new Date();
newCooldown.setMinutes(newCooldown.getMinutes() + 5); newCooldown.setMinutes(newCooldown.getMinutes() + 5);
db.run("UPDATE accounts SET cooldown = ? WHERE id = ?", newCooldown.toISOString() ,accountNumber, (err) => { db.run("UPDATE accounts SET cooldown = ? WHERE id = ?", newCooldown.toISOString(), accountNumber, (err) => {
if (err) { if (err) {
console.error(err); console.error(err);
} }
@ -735,7 +735,7 @@ app.post("/api/v1/webhook/:brand/:accountNumber", (req, res) => {
// send alert to accountNumber // send alert to accountNumber
sendAlert(req.params.accountNumber, req.body.transaction, req.body.placeName, req.body.systemName, req.body.zoneNumber, req.body.zoneName, req.body.event).then((resp) => { sendAlert(req.params.accountNumber, req.body.transaction, req.body.placeName, req.body.systemName, req.body.zoneNumber, req.body.zoneName, req.body.event).then((resp) => {
if(resp == "Cooldown") { if (resp == "Cooldown") {
return res.status(429).send("Cooldown"); return res.status(429).send("Cooldown");
} }
res.status(204).send(); res.status(204).send();
@ -773,14 +773,14 @@ app.ws("/api/v1/websocket/:accountNumber", (ws, req) => {
db.get("SELECT * FROM accounts WHERE id = ? AND verified = 1", req.params.accountNumber, (err, row) => { db.get("SELECT * FROM accounts WHERE id = ? AND verified = 1", req.params.accountNumber, (err, row) => {
if (err) { if (err) {
console.error(err); console.error(err);
ws.send(JSON.stringify({status: "error", code: 500, message: "Internal Server Error", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ status: "error", code: 500, message: "Internal Server Error", timestamp: new Date().toISOString() }));
ws.close(); ws.close();
return; return;
} else if (row) { } else if (row) {
userData = row; userData = row;
ws.send(JSON.stringify({status: "connected", code: 200, timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ status: "connected", code: 200, timestamp: new Date().toISOString() }));
} else { } else {
ws.send(JSON.stringify({status: "error", code: 404, message: "Account not found or not verified", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ status: "error", code: 404, message: "Account not found or not verified", timestamp: new Date().toISOString() }));
ws.close(); ws.close();
return; return;
} }
@ -792,25 +792,25 @@ app.ws("/api/v1/websocket/:accountNumber", (ws, req) => {
msg = JSON.parse(msg); msg = JSON.parse(msg);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
ws.send(JSON.stringify({status: "error", message: "Invalid JSON", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ status: "error", message: "Invalid JSON", timestamp: new Date().toISOString() }));
return; return;
} }
switch(msg.action) { switch (msg.action) {
case "close": case "close":
ws.send(JSON.stringify({response: "closed", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ response: "closed", timestamp: new Date().toISOString() }));
ws.close(); ws.close();
break; break;
case "ping": case "ping":
ws.send(JSON.stringify({response: "pong", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ response: "pong", timestamp: new Date().toISOString() }));
break; break;
case "getStatus": case "getStatus":
// Get the armState column from the account database // Get the armState column from the account database
db.get("SELECT armState FROM accounts WHERE id = ?", req.params.accountNumber, (err, row) => { db.get("SELECT armState FROM accounts WHERE id = ?", req.params.accountNumber, (err, row) => {
if (err) { if (err) {
console.error(err); console.error(err);
ws.send(JSON.stringify({action: msg.action, status: "error", code: 500, message: "Internal Server Error", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ action: msg.action, status: "error", code: 500, message: "Internal Server Error", timestamp: new Date().toISOString() }));
} else { } else {
ws.send(JSON.stringify({action: msg.action, status: "success", code: 200, armState: row.armState, timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ action: msg.action, status: "success", code: 200, armState: row.armState, timestamp: new Date().toISOString() }));
} }
}); });
break; break;
@ -846,9 +846,9 @@ app.ws("/api/v1/websocket/:accountNumber", (ws, req) => {
db.run("UPDATE accounts SET armState = ? WHERE id = ?", msg.armState, req.params.accountNumber, (err) => { db.run("UPDATE accounts SET armState = ? WHERE id = ?", msg.armState, req.params.accountNumber, (err) => {
if (err) { if (err) {
console.error(err); console.error(err);
ws.send(JSON.stringify({action: msg.action, status: "error", code: 500, message: "Internal Server Error", timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ action: msg.action, status: "error", code: 500, message: "Internal Server Error", timestamp: new Date().toISOString() }));
} else { } else {
ws.send(JSON.stringify({action: msg.action, status: "success", code: 200, timestamp: new Date().toISOString()})); ws.send(JSON.stringify({ action: msg.action, status: "success", code: 200, timestamp: new Date().toISOString() }));
} }
}); });
break; break;
@ -857,5 +857,17 @@ app.ws("/api/v1/websocket/:accountNumber", (ws, req) => {
}); });
}); });
app.get('/api/healthcheck', (req, res) => {
// Check if the bot is logged in and connected to Discord
const botHealthy = client.isReady();
// Respond with the health check status
res.json({
healthy: botHealthy,
botStatus: botHealthy ? 'Bot is connected and healthy' : 'Bot is not connected or has failed',
timestamp: new Date(),
});
});
startTime = new Date(); startTime = new Date();
client.login(process.env.DISCORD_TOKEN); client.login(process.env.DISCORD_TOKEN);

View file

@ -0,0 +1,17 @@
[
{
"name": "Piper - Amy (Default)",
"value": "0",
"command": "piper -m /opt/alarm-monitoring/tts/en_US-amy-medium.onnx -f %s"
},
{
"name": "Flite - OG SecuriNet Voice",
"value": "1",
"command": "flite -o %s"
},
{
"name": "DECTalk Paul",
"value": "2",
"command": "/usr/bin/dectalk/say -fo %s -"
}
]