From 8167debbe9e63029608f38978bcdd7ce1336439d Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 27 May 2020 15:18:04 +0700 Subject: [PATCH] Expose git commit hash to backend info api. --- backend/src/api/backend-info.ts | 29 ++++++++++++++++++++++++++++ backend/src/api/websocket-handler.ts | 19 ++++-------------- backend/src/routes.ts | 6 ++---- 3 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 backend/src/api/backend-info.ts diff --git a/backend/src/api/backend-info.ts b/backend/src/api/backend-info.ts new file mode 100644 index 000000000..8f028bac8 --- /dev/null +++ b/backend/src/api/backend-info.ts @@ -0,0 +1,29 @@ +import * as fs from 'fs'; +import * as os from 'os'; + +class BackendInfo { + gitCommitHash = ''; + hostname = ''; + + constructor() { + this.setLatestCommitHash(); + this.hostname = os.hostname(); + } + + public getBackendInfo() { + return { + 'hostname': this.hostname, + 'git-commit': this.gitCommitHash, + }; + } + + private setLatestCommitHash(): void { + try { + this.gitCommitHash = fs.readFileSync('../.git/refs/heads/master').toString().trim(); + } catch (e) { + console.log('Could not load git commit info, skipping.'); + } + } +} + +export default new BackendInfo(); diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index a80768957..339e8efd5 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -1,30 +1,19 @@ const config = require('../../mempool-config.json'); import * as WebSocket from 'ws'; -import * as fs from 'fs'; import { Block, TransactionExtended, Statistic, WebsocketResponse } from '../interfaces'; import blocks from './blocks'; import memPool from './mempool'; +import backendInfo from './backend-info'; import mempoolBlocks from './mempool-blocks'; import fiatConversion from './fiat-conversion'; import * as os from 'os'; class WebsocketHandler { private wss: WebSocket.Server | undefined; - private latestGitCommitHash = ''; private nativeAssetId = '6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d'; - constructor() { - this.setLatestGitCommit(); - } - - setLatestGitCommit() { - try { - this.latestGitCommitHash = fs.readFileSync('../.git/refs/heads/master').toString().trim(); - } catch (e) { - console.log('Could not load git commit info, skipping.'); - } - } + constructor() { } setWebsocketServer(wss: WebSocket.Server) { this.wss = wss; @@ -93,8 +82,8 @@ class WebsocketHandler { 'blocks': _blocks.slice(Math.max(_blocks.length - config.INITIAL_BLOCK_AMOUNT, 0)), 'conversions': fiatConversion.getTickers()['BTCUSD'], 'mempool-blocks': mempoolBlocks.getMempoolBlocks(), - 'git-commit': this.latestGitCommitHash, - 'hostname': os.hostname(), + 'git-commit': backendInfo.gitCommitHash, + 'hostname': backendInfo.hostname, })); } diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 397a9ce02..20df6ca87 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -1,8 +1,8 @@ import statistics from './api/statistics'; import feeApi from './api/fee-api'; +import backendInfo from './api/backend-info'; import mempoolBlocks from './api/mempool-blocks'; import mempool from './api/mempool'; -import * as os from 'os'; class Routes { private cache = {}; @@ -76,9 +76,7 @@ class Routes { } public getBackendInfo(req, res) { - res.send({ - 'hostname': os.hostname(), - }); + res.send(backendInfo.getBackendInfo()); } }