Add healthcheck endpoint
This commit is contained in:
parent
fdc05cd165
commit
a32f3e09c0
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -133,3 +133,4 @@ dist
|
|||
database.db
|
||||
tts/*
|
||||
!tts/.gitkeep
|
||||
tts.json
|
40
index.js
40
index.js
|
@ -188,7 +188,7 @@ function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber
|
|||
newCooldown = new Date();
|
||||
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) {
|
||||
console.error(err);
|
||||
}
|
||||
|
@ -326,7 +326,7 @@ function sendVerificationCode(account) {
|
|||
newCooldown = new Date();
|
||||
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) {
|
||||
console.error(err);
|
||||
}
|
||||
|
@ -735,7 +735,7 @@ app.post("/api/v1/webhook/:brand/:accountNumber", (req, res) => {
|
|||
|
||||
// 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) => {
|
||||
if(resp == "Cooldown") {
|
||||
if (resp == "Cooldown") {
|
||||
return res.status(429).send("Cooldown");
|
||||
}
|
||||
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) => {
|
||||
if (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();
|
||||
return;
|
||||
} else if (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 {
|
||||
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();
|
||||
return;
|
||||
}
|
||||
|
@ -792,25 +792,25 @@ app.ws("/api/v1/websocket/:accountNumber", (ws, req) => {
|
|||
msg = JSON.parse(msg);
|
||||
} catch (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;
|
||||
}
|
||||
switch(msg.action) {
|
||||
switch (msg.action) {
|
||||
case "close":
|
||||
ws.send(JSON.stringify({response: "closed", timestamp: new Date().toISOString()}));
|
||||
ws.send(JSON.stringify({ response: "closed", timestamp: new Date().toISOString() }));
|
||||
ws.close();
|
||||
break;
|
||||
case "ping":
|
||||
ws.send(JSON.stringify({response: "pong", timestamp: new Date().toISOString()}));
|
||||
ws.send(JSON.stringify({ response: "pong", timestamp: new Date().toISOString() }));
|
||||
break;
|
||||
case "getStatus":
|
||||
// Get the armState column from the account database
|
||||
db.get("SELECT armState FROM accounts WHERE id = ?", req.params.accountNumber, (err, row) => {
|
||||
if (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 {
|
||||
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;
|
||||
|
@ -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) => {
|
||||
if (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 {
|
||||
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;
|
||||
|
@ -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();
|
||||
client.login(process.env.DISCORD_TOKEN);
|
|
@ -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 -"
|
||||
}
|
||||
]
|
Loading…
Reference in a new issue