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-02-16 16:15:07 +01:00
|
|
|
import routes from './routes';
|
2019-07-21 16:59:47 +02:00
|
|
|
import blocks from './api/blocks';
|
2020-02-16 16:15:07 +01:00
|
|
|
import memPool from './api/mempool';
|
|
|
|
import diskCache from './api/disk-cache';
|
2019-07-21 16:59:47 +02:00
|
|
|
import statistics from './api/statistics';
|
2020-02-26 11:49:53 +01:00
|
|
|
import websocketHandler from './api/websocket-handler';
|
2019-07-21 16:59:47 +02:00
|
|
|
import fiatConversion from './api/fiat-conversion';
|
2020-07-03 18:45:19 +02:00
|
|
|
import bisq from './api/bisq';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
class Server {
|
2020-02-26 11:49:53 +01:00
|
|
|
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();
|
2020-02-16 16:15:07 +01:00
|
|
|
|
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());
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
if (config.SSL === true) {
|
2019-07-21 16:59:47 +02:00
|
|
|
const credentials = {
|
2020-02-16 16:15:07 +01:00
|
|
|
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 });
|
2020-02-16 16:15:07 +01:00
|
|
|
} else {
|
|
|
|
this.server = http.createServer(this.app);
|
|
|
|
this.wss = new WebSocket.Server({ server: this.server });
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
this.setUpHttpApiRoutes();
|
2019-07-21 16:59:47 +02:00
|
|
|
this.setUpWebsocketHandling();
|
|
|
|
this.runMempoolIntervalFunctions();
|
|
|
|
|
|
|
|
statistics.startStatistics();
|
|
|
|
fiatConversion.startService();
|
2020-02-26 11:49:53 +01:00
|
|
|
diskCache.loadMempoolCache();
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-07-03 18:45:19 +02:00
|
|
|
if (config.BISQ_ENABLED) {
|
|
|
|
bisq.startBisqService();
|
2020-07-14 16:26:02 +02:00
|
|
|
bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price));
|
2020-07-03 18:45:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
this.server.listen(config.HTTP_PORT, () => {
|
|
|
|
console.log(`Server started on port ${config.HTTP_PORT}`);
|
2019-07-21 16:59:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01: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();
|
2020-02-16 16:15:07 +01:00
|
|
|
setTimeout(this.runMempoolIntervalFunctions.bind(this), config.ELECTRS_POLL_RATE_MS);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01: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
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
setUpHttpApiRoutes() {
|
2019-07-21 16:59:47 +02:00
|
|
|
this.app
|
2020-02-27 19:09:07 +01:00
|
|
|
.get(config.API_ENDPOINT + 'transaction-times', routes.getTransactionTimes)
|
2019-07-21 16:59:47 +02:00
|
|
|
.get(config.API_ENDPOINT + 'fees/recommended', routes.getRecommendedFees)
|
2020-02-16 16:15:07 +01:00
|
|
|
.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)
|
2019-08-15 13:06:08 +02:00
|
|
|
.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)
|
2020-02-26 11:49:53 +01:00
|
|
|
;
|
2020-07-03 18:45:19 +02:00
|
|
|
|
|
|
|
if (config.BISQ_ENABLED) {
|
|
|
|
this.app
|
2020-07-14 09:38:52 +02:00
|
|
|
.get(config.API_ENDPOINT + 'bisq/stats', routes.getBisqStats)
|
2020-07-03 18:45:19 +02:00
|
|
|
.get(config.API_ENDPOINT + 'bisq/tx/:txId', routes.getBisqTransaction)
|
|
|
|
.get(config.API_ENDPOINT + 'bisq/block/:hash', routes.getBisqBlock)
|
2020-07-15 08:10:13 +02:00
|
|
|
.get(config.API_ENDPOINT + 'bisq/blocks/tip/height', routes.getBisqTip)
|
2020-07-13 10:16:12 +02:00
|
|
|
.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)
|
2020-07-03 18:45:19 +02:00
|
|
|
.get(config.API_ENDPOINT + 'bisq/txs/:index/:length', routes.getBisqTransactions)
|
|
|
|
;
|
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
2019-11-06 08:35:02 +01:00
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
const server = new Server();
|