Add health + stats endpoint

This commit is contained in:
Wietse Wind
2023-10-06 01:42:24 +02:00
parent e405e4d561
commit 9d1b9557d4
4 changed files with 40 additions and 0 deletions

View File

@@ -5,6 +5,12 @@ import expressWs from 'express-ws'
import autoindex from 'express-autoindex/dist/index.cjs.js'
import 'dotenv/config'
import { lastLedger } from '../lib/onLedger.mjs'
import { txCount } from '../lib/onTransaction.mjs'
const startDate = new Date()
let lastLedgerTx
let wss // WebSocket Server
if (!wss) {
@@ -27,6 +33,15 @@ if (!wss) {
app.use('/', express.static('./store/'))
app.use('/health', (req, res) => {
res.json({
uptime: new Date() - startDate,
lastLedger: lastLedger ?? null,
lastLedgerTx: lastLedgerTx ?? null,
txCount: txCount ?? null,
})
})
app.use('/:networkId([0-9]{1,})', (req, res, next) => {
return autoindex('./store/' + req.params.networkId + '/', { json: /application\/json/.test(req.get('accept')) })(req, res, next)
})
@@ -78,6 +93,8 @@ const emit = _data => {
data.xpop.blob = undefined
}
lastLedgerTx = data.origin.ledgerIndex
if (account === '' || data.account === account) {
client.send(JSON.stringify(data), { binary: false })
}