Expose git commit hash to backend info api.

This commit is contained in:
softsimon 2020-05-27 15:18:04 +07:00
parent 4d4b6f4831
commit 8167debbe9
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
3 changed files with 35 additions and 19 deletions

View File

@ -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();

View File

@ -1,30 +1,19 @@
const config = require('../../mempool-config.json'); const config = require('../../mempool-config.json');
import * as WebSocket from 'ws'; import * as WebSocket from 'ws';
import * as fs from 'fs';
import { Block, TransactionExtended, Statistic, WebsocketResponse } from '../interfaces'; import { Block, TransactionExtended, Statistic, WebsocketResponse } from '../interfaces';
import blocks from './blocks'; import blocks from './blocks';
import memPool from './mempool'; import memPool from './mempool';
import backendInfo from './backend-info';
import mempoolBlocks from './mempool-blocks'; import mempoolBlocks from './mempool-blocks';
import fiatConversion from './fiat-conversion'; import fiatConversion from './fiat-conversion';
import * as os from 'os'; import * as os from 'os';
class WebsocketHandler { class WebsocketHandler {
private wss: WebSocket.Server | undefined; private wss: WebSocket.Server | undefined;
private latestGitCommitHash = '';
private nativeAssetId = '6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d'; private nativeAssetId = '6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d';
constructor() { 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.');
}
}
setWebsocketServer(wss: WebSocket.Server) { setWebsocketServer(wss: WebSocket.Server) {
this.wss = wss; this.wss = wss;
@ -93,8 +82,8 @@ class WebsocketHandler {
'blocks': _blocks.slice(Math.max(_blocks.length - config.INITIAL_BLOCK_AMOUNT, 0)), 'blocks': _blocks.slice(Math.max(_blocks.length - config.INITIAL_BLOCK_AMOUNT, 0)),
'conversions': fiatConversion.getTickers()['BTCUSD'], 'conversions': fiatConversion.getTickers()['BTCUSD'],
'mempool-blocks': mempoolBlocks.getMempoolBlocks(), 'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
'git-commit': this.latestGitCommitHash, 'git-commit': backendInfo.gitCommitHash,
'hostname': os.hostname(), 'hostname': backendInfo.hostname,
})); }));
} }

View File

@ -1,8 +1,8 @@
import statistics from './api/statistics'; import statistics from './api/statistics';
import feeApi from './api/fee-api'; import feeApi from './api/fee-api';
import backendInfo from './api/backend-info';
import mempoolBlocks from './api/mempool-blocks'; import mempoolBlocks from './api/mempool-blocks';
import mempool from './api/mempool'; import mempool from './api/mempool';
import * as os from 'os';
class Routes { class Routes {
private cache = {}; private cache = {};
@ -76,9 +76,7 @@ class Routes {
} }
public getBackendInfo(req, res) { public getBackendInfo(req, res) {
res.send({ res.send(backendInfo.getBackendInfo());
'hostname': os.hostname(),
});
} }
} }