generated from digitalcredentials/isomorphic-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from digitalcredentials/jc-add-healthz
add healthz endpoint
- Loading branch information
Showing
19 changed files
with
735 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
**/*.env | ||
.git | ||
.github | ||
.husky | ||
coverage | ||
logs | ||
node_modules | ||
.dockerignore | ||
.editorconfig | ||
.eslintrc.cjs | ||
.gitignore | ||
.lintstagedrc.json | ||
.prettierignore | ||
.prettierrc.js | ||
compose-test.yaml | ||
Dockerfile | ||
README | ||
.env.healthcheck.testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,9 @@ | ||
#!/bin/sh | ||
if [ -z "$husky_skip_init" ]; then | ||
debug () { | ||
[ "$HUSKY_DEBUG" = "1" ] && echo "husky (debug) - $1" | ||
} | ||
echo "husky - DEPRECATED | ||
readonly hook_name="$(basename "$0")" | ||
debug "starting $hook_name..." | ||
Please remove the following two lines from $0: | ||
if [ "$HUSKY" = "0" ]; then | ||
debug "HUSKY env variable is set to 0, skipping hook" | ||
exit 0 | ||
fi | ||
#!/usr/bin/env sh | ||
. \"\$(dirname -- \"\$0\")/_/husky.sh\" | ||
if [ -f ~/.huskyrc ]; then | ||
debug "sourcing ~/.huskyrc" | ||
. ~/.huskyrc | ||
fi | ||
|
||
export readonly husky_skip_init=1 | ||
sh -e "$0" "$@" | ||
exitCode="$?" | ||
|
||
if [ $exitCode != 0 ]; then | ||
echo "husky - $hook_name hook exited with code $exitCode (error)" | ||
exit $exitCode | ||
fi | ||
|
||
exit 0 | ||
fi | ||
They WILL FAIL in v10.0.0 | ||
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npx lint-staged | ||
npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"*.js": [ | ||
"prettier --write", | ||
"eslint" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build | ||
coverage | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
trailingComma: 'none', | ||
tabWidth: 2, | ||
semi: false, | ||
singleQuote: true, | ||
bracketSpacing: true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import globals from 'globals' | ||
import pluginJs from '@eslint/js' | ||
import eslintConfigPrettier from 'eslint-config-prettier' | ||
import mochaPlugin from 'eslint-plugin-mocha' | ||
|
||
export default [ | ||
{ languageOptions: { globals: globals.node } }, | ||
mochaPlugin.configs.flat.recommended, | ||
pluginJs.configs.recommended, | ||
eslintConfigPrettier | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import nodemailer from 'nodemailer' | ||
import axios from 'axios' | ||
|
||
const serviceURL = process.env.HEALTH_CHECK_SERVICE_URL | ||
const serviceName = process.env.HEALTH_CHECK_SERVICE_NAME | ||
const shouldPostToWebHook = process.env.HEALTH_CHECK_WEB_HOOK | ||
const shouldSendEmail = | ||
process.env.HEALTH_CHECK_SMTP_HOST && | ||
process.env.HEALTH_CHECK_SMTP_USER && | ||
process.env.HEALTH_CHECK_SMTP_PASS && | ||
process.env.HEALTH_CHECK_EMAIL_FROM && | ||
process.env.HEALTH_CHECK_EMAIL_RECIPIENT | ||
|
||
axios | ||
.get(serviceURL) | ||
.then(async function (response) { | ||
try { | ||
const body = response.data | ||
if (body.healthy === true) { | ||
process.exit(0) | ||
} | ||
await notify(`${serviceName} is unhealthy: ${body.error}`) | ||
process.exit(1) | ||
} catch (error) { | ||
await notify( | ||
`${serviceName} is potentially unhealthy - error: ${JSON.stringify(error)}` | ||
) | ||
process.exit(1) | ||
} | ||
}) | ||
.catch(async (error) => { | ||
await notify( | ||
`${serviceName} is unhealthy and will restart after 3 tries. Error: ${error.message}` | ||
) | ||
process.exit(1) | ||
}) | ||
|
||
async function notify(message) { | ||
console.log(message) | ||
if (shouldSendEmail) await sendMail(message) | ||
if (shouldPostToWebHook) await postToWebHook(message) | ||
} | ||
|
||
async function postToWebHook(text) { | ||
await axios | ||
.post(process.env.HEALTH_CHECK_WEB_HOOK, { text }) | ||
.catch((error) => { | ||
console.error(error) | ||
}) | ||
} | ||
|
||
async function sendMail(message) { | ||
const messageParams = { | ||
from: process.env.HEALTH_CHECK_EMAIL_FROM, | ||
to: process.env.HEALTH_CHECK_EMAIL_RECIPIENT, | ||
subject: process.env.HEALTH_CHECK_EMAIL_SUBJECT, | ||
text: message | ||
} | ||
|
||
const mailTransport = { | ||
host: process.env.HEALTH_CHECK_SMTP_HOST, | ||
auth: { | ||
user: process.env.HEALTH_CHECK_SMTP_USER, | ||
pass: process.env.HEALTH_CHECK_SMTP_PASS | ||
}, | ||
...(process.env.HEALTH_CHECK_SMTP_PORT && { | ||
port: process.env.HEALTH_CHECK_SMTP_PORT | ||
}) | ||
} | ||
|
||
const transporter = nodemailer.createTransport(mailTransport) | ||
|
||
try { | ||
await transporter.sendMail(messageParams) | ||
} catch (error) { | ||
console.log('the email send error: ') | ||
console.log(error) | ||
} | ||
} | ||
|
||
//import * as http from 'node:http'; | ||
/* const options = { hostname: 'SIGNER', port: (process.env.PORT || 4006), path: '/healthz', method: 'GET' }; | ||
http | ||
.request(options, (res) => { | ||
let body = ''; | ||
res.on('data', (chunk) => { | ||
body += chunk; | ||
}); | ||
res.on('end', async () => { | ||
try { | ||
const response = JSON.parse(body); | ||
if (response.healthy === true) { | ||
console.log('healthy response received: ', body); | ||
await sendMail("It worked!") | ||
process.exit(0); | ||
} | ||
console.log('Unhealthy response received: ', body); | ||
await sendMail(`It worked, but with error: ${JSON.stringify(body)}`) | ||
process.exit(1); | ||
} catch (err) { | ||
console.log('Error parsing JSON response body: ', err); | ||
process.exit(1); | ||
} | ||
}); | ||
}) | ||
.on('error', (err) => { | ||
console.log('Error: ', err); | ||
process.exit(1); | ||
}) | ||
.end(); */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function TransactionException(code, message, stack) { | ||
this.code = code | ||
this.message = message | ||
this.stack = stack | ||
} | ||
|
Oops, something went wrong.