mempool/backend/src/index.ts

114 lines
4.2 KiB
TypeScript
Raw Normal View History

2019-07-21 16:59:47 +02:00
const config = require('../mempool-config.json');
2020-06-07 12:30:32 +02:00
import { Express, Request, Response, NextFunction } from 'express';
2019-07-21 16:59:47 +02:00
import * as fs from 'fs';
import * as express from 'express';
import * as compression from 'compression';
import * as http from 'http';
import * as https from 'https';
import * as WebSocket from 'ws';
2020-07-22 19:04:29 +02:00
import { checkDbConnection } from './database';
import routes from './routes';
2019-07-21 16:59:47 +02:00
import blocks from './api/blocks';
import memPool from './api/mempool';
import diskCache from './api/disk-cache';
2019-07-21 16:59:47 +02:00
import statistics from './api/statistics';
import websocketHandler from './api/websocket-handler';
2019-07-21 16:59:47 +02:00
import fiatConversion from './api/fiat-conversion';
import bisq from './api/bisq';
2019-07-21 16:59:47 +02:00
class Server {
wss: WebSocket.Server;
server: https.Server | http.Server;
2020-06-07 12:30:32 +02:00
app: Express;
2019-07-21 16:59:47 +02:00
constructor() {
this.app = express();
2019-07-21 16:59:47 +02:00
this.app
2020-06-07 12:30:32 +02:00
.use((req: Request, res: Response, next: NextFunction) => {
2019-07-21 16:59:47 +02:00
res.setHeader('Access-Control-Allow-Origin', '*');
next();
})
.use(compression());
if (config.SSL === true) {
2019-07-21 16:59:47 +02:00
const credentials = {
cert: fs.readFileSync(config.SSL_CERT_FILE_PATH),
key: fs.readFileSync(config.SSL_KEY_FILE_PATH),
2019-07-21 16:59:47 +02:00
};
this.server = https.createServer(credentials, this.app);
this.wss = new WebSocket.Server({ server: this.server });
} else {
this.server = http.createServer(this.app);
this.wss = new WebSocket.Server({ server: this.server });
2019-07-21 16:59:47 +02:00
}
2020-07-22 19:04:29 +02:00
if (!config.DB_DISABLED) {
checkDbConnection();
statistics.startStatistics();
}
this.setUpHttpApiRoutes();
2019-07-21 16:59:47 +02:00
this.setUpWebsocketHandling();
this.runMempoolIntervalFunctions();
fiatConversion.startService();
diskCache.loadMempoolCache();
2019-07-21 16:59:47 +02:00
if (config.BISQ_ENABLED) {
bisq.startBisqService();
2020-07-14 16:26:02 +02:00
bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price));
}
this.server.listen(config.HTTP_PORT, () => {
console.log(`Server started on port ${config.HTTP_PORT}`);
2019-07-21 16:59:47 +02:00
});
}
async runMempoolIntervalFunctions() {
2020-02-17 14:39:20 +01:00
await memPool.updateMemPoolInfo();
2019-07-21 16:59:47 +02:00
await blocks.updateBlocks();
await memPool.updateMempool();
setTimeout(this.runMempoolIntervalFunctions.bind(this), config.ELECTRS_POLL_RATE_MS);
2019-07-21 16:59:47 +02:00
}
setUpWebsocketHandling() {
websocketHandler.setWebsocketServer(this.wss);
websocketHandler.setupConnectionHandling();
statistics.setNewStatisticsEntryCallback(websocketHandler.handleNewStatistic.bind(websocketHandler));
blocks.setNewBlockCallback(websocketHandler.handleNewBlock.bind(websocketHandler));
memPool.setMempoolChangedCallback(websocketHandler.handleMempoolChange.bind(websocketHandler));
2019-07-21 16:59:47 +02:00
}
setUpHttpApiRoutes() {
2019-07-21 16:59:47 +02:00
this.app
.get(config.API_ENDPOINT + 'transaction-times', routes.getTransactionTimes)
2019-07-21 16:59:47 +02:00
.get(config.API_ENDPOINT + 'fees/recommended', routes.getRecommendedFees)
.get(config.API_ENDPOINT + 'fees/mempool-blocks', routes.getMempoolBlocks)
2019-07-21 16:59:47 +02:00
.get(config.API_ENDPOINT + 'statistics/2h', routes.get2HStatistics)
.get(config.API_ENDPOINT + 'statistics/24h', routes.get24HStatistics.bind(routes))
.get(config.API_ENDPOINT + 'statistics/1w', routes.get1WHStatistics.bind(routes))
.get(config.API_ENDPOINT + 'statistics/1m', routes.get1MStatistics.bind(routes))
.get(config.API_ENDPOINT + 'statistics/3m', routes.get3MStatistics.bind(routes))
.get(config.API_ENDPOINT + 'statistics/6m', routes.get6MStatistics.bind(routes))
2020-02-16 18:26:57 +01:00
.get(config.API_ENDPOINT + 'statistics/1y', routes.get1YStatistics.bind(routes))
2020-05-26 13:06:14 +02:00
.get(config.API_ENDPOINT + 'backend-info', routes.getBackendInfo)
;
if (config.BISQ_ENABLED) {
this.app
2020-07-14 09:38:52 +02:00
.get(config.API_ENDPOINT + 'bisq/stats', routes.getBisqStats)
.get(config.API_ENDPOINT + 'bisq/tx/:txId', routes.getBisqTransaction)
.get(config.API_ENDPOINT + 'bisq/block/:hash', routes.getBisqBlock)
.get(config.API_ENDPOINT + 'bisq/blocks/tip/height', routes.getBisqTip)
.get(config.API_ENDPOINT + 'bisq/blocks/:index/:length', routes.getBisqBlocks)
2020-07-13 16:46:25 +02:00
.get(config.API_ENDPOINT + 'bisq/address/:address', routes.getBisqAddress)
.get(config.API_ENDPOINT + 'bisq/txs/:index/:length', routes.getBisqTransactions)
;
}
}
}
2019-11-06 08:35:02 +01:00
const server = new Server();