2020-05-27 10:18:04 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
2020-10-13 10:27:52 +02:00
|
|
|
import logger from '../logger';
|
2021-04-12 20:17:13 +02:00
|
|
|
import { IBackendInfo } from '../mempool.interfaces';
|
2020-05-27 10:18:04 +02:00
|
|
|
|
|
|
|
class BackendInfo {
|
2021-04-12 20:17:13 +02:00
|
|
|
private gitCommitHash = '';
|
|
|
|
private hostname = '';
|
|
|
|
private version = '';
|
2020-05-27 10:18:04 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.setLatestCommitHash();
|
2021-04-12 20:17:13 +02:00
|
|
|
this.setVersion();
|
2020-05-27 10:18:04 +02:00
|
|
|
this.hostname = os.hostname();
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:17:13 +02:00
|
|
|
public getBackendInfo(): IBackendInfo {
|
2020-05-27 10:18:04 +02:00
|
|
|
return {
|
2021-04-12 20:17:13 +02:00
|
|
|
hostname: this.hostname,
|
|
|
|
gitCommit: this.gitCommitHash,
|
|
|
|
version: this.version,
|
2020-05-27 10:18:04 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-13 12:48:43 +02:00
|
|
|
public getShortCommitHash() {
|
|
|
|
return this.gitCommitHash.slice(0, 7);
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:18:04 +02:00
|
|
|
private setLatestCommitHash(): void {
|
|
|
|
try {
|
|
|
|
this.gitCommitHash = fs.readFileSync('../.git/refs/heads/master').toString().trim();
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
logger.err('Could not load git commit info: ' + (e instanceof Error ? e.message : e));
|
2020-05-27 10:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 20:17:13 +02:00
|
|
|
|
|
|
|
private setVersion(): void {
|
|
|
|
try {
|
|
|
|
const packageJson = fs.readFileSync('package.json').toString();
|
|
|
|
this.version = JSON.parse(packageJson).version;
|
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
throw new Error(e instanceof Error ? e.message : 'Error');
|
2021-04-12 20:17:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 10:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new BackendInfo();
|