Add version to health & homempage response

This commit is contained in:
Wietse Wind
2023-10-09 12:39:12 +02:00
parent 9388706405
commit 0bcaf4cb69

View File

@@ -1,4 +1,5 @@
import { WebSocket } from 'ws'
import { readFile } from 'fs'
import morgan from 'morgan'
import express from 'express'
import expressWs from 'express-ws'
@@ -19,6 +20,23 @@ const telemetry = {
sent: false,
}
const version = await new Promise(resolve => {
readFile(new URL('../package.json', import.meta.url).pathname, (err, data) => {
if (!err) {
try {
const pjson = JSON.parse(data)
return resolve(pjson?.version)
}
catch (e) {
//
}
}
resolve(null)
})
})
console.log('Running backend version', version || '<< UNKNOWN >>')
const sendTelemetry = async () => {
if (process.env?.TELEMETRY === 'YES') {
try {
@@ -98,11 +116,13 @@ if (!wss) {
unlkey: process.env?.UNLKEY ?? null,
},
config: {
version,
networkid: process.env?.NETWORKID ?? null,
urlprefix: process.env?.URL_PREFIX ?? null,
requiredTxFields: typeof process.env?.FIELDSREQUIRED === 'string' ? process.env.FIELDSREQUIRED.split(',') : null,
},
stats: {
uptime: new Date() - startDate,
lastLedger: lastLedger ?? null,
lastWsPushedLedger: lastWsPushedLedger ?? null,
txCount: txCount ?? null,
@@ -118,6 +138,7 @@ if (!wss) {
app.use('/health', (req, res) => {
res.setHeader('content-type', 'application/json')
res.json({
version,
uptime: new Date() - startDate,
lastLedger: lastLedger ?? null,
lastWsPushedLedger: lastWsPushedLedger ?? null,