diff --git a/backend/src/api/acceleration/acceleration.routes.ts b/backend/src/api/acceleration/acceleration.routes.ts new file mode 100644 index 000000000..69b320171 --- /dev/null +++ b/backend/src/api/acceleration/acceleration.routes.ts @@ -0,0 +1,75 @@ +import { Application, Request, Response } from "express"; +import config from "../../config"; +import axios from "axios"; +import logger from "../../logger"; + +class AccelerationRoutes { + private tag = 'Accelerator'; + + public initRoutes(app: Application) { + app + .get(config.MEMPOOL.API_URL_PREFIX + 'services/accelerator/accelerations', this.$getAcceleratorAccelerations.bind(this)) + .get(config.MEMPOOL.API_URL_PREFIX + 'services/accelerator/accelerations/history', this.$getAcceleratorAccelerationsHistory.bind(this)) + .get(config.MEMPOOL.API_URL_PREFIX + 'services/accelerator/accelerations/history/aggregated', this.$getAcceleratorAccelerationsHistoryAggregated.bind(this)) + .get(config.MEMPOOL.API_URL_PREFIX + 'services/accelerator/accelerations/stats', this.$getAcceleratorAccelerationsStats.bind(this)) + ; + } + + private async $getAcceleratorAccelerations(req: Request, res: Response) { + const url = `${config.MEMPOOL_SERVICES.API}/${req.originalUrl.replace('/api/v1/services/', '')}`; + try { + const response = await axios.get(url, { responseType: 'stream', timeout: 10000 }); + for (const key in response.headers) { + res.setHeader(key, response.headers[key]); + } + response.data.pipe(res); + } catch (e) { + logger.err(`Unable to get current accelerations from ${url} in $getAcceleratorAccelerations(), ${e}`, this.tag); + res.status(500).end(); + } + } + + private async $getAcceleratorAccelerationsHistory(req: Request, res: Response) { + const url = `${config.MEMPOOL_SERVICES.API}/${req.originalUrl.replace('/api/v1/services/', '')}`; + try { + const response = await axios.get(url, { responseType: 'stream', timeout: 10000 }); + for (const key in response.headers) { + res.setHeader(key, response.headers[key]); + } + response.data.pipe(res); + } catch (e) { + logger.err(`Unable to get acceleration history from ${url} in $getAcceleratorAccelerationsHistory(), ${e}`, this.tag); + res.status(500).end(); + } + } + + private async $getAcceleratorAccelerationsHistoryAggregated(req: Request, res: Response) { + const url = `${config.MEMPOOL_SERVICES.API}/${req.originalUrl.replace('/api/v1/services/', '')}`; + try { + const response = await axios.get(url, { responseType: 'stream', timeout: 10000 }); + for (const key in response.headers) { + res.setHeader(key, response.headers[key]); + } + response.data.pipe(res); + } catch (e) { + logger.err(`Unable to get aggregated acceleration history from ${url} in $getAcceleratorAccelerationsHistoryAggregated(), ${e}`, this.tag); + res.status(500).end(); + } + } + + private async $getAcceleratorAccelerationsStats(req: Request, res: Response) { + const url = `${config.MEMPOOL_SERVICES.API}/${req.originalUrl.replace('/api/v1/services/', '')}`; + try { + const response = await axios.get(url, { responseType: 'stream', timeout: 10000 }); + for (const key in response.headers) { + res.setHeader(key, response.headers[key]); + } + response.data.pipe(res); + } catch (e) { + logger.err(`Unable to get acceleration stats from ${url} in $getAcceleratorAccelerationsStats(), ${e}`, this.tag); + res.status(500).end(); + } + } +} + +export default new AccelerationRoutes(); \ No newline at end of file diff --git a/backend/src/api/acceleration.ts b/backend/src/api/acceleration/acceleration.ts similarity index 99% rename from backend/src/api/acceleration.ts rename to backend/src/api/acceleration/acceleration.ts index 412d65231..2dbaa8b07 100644 --- a/backend/src/api/acceleration.ts +++ b/backend/src/api/acceleration/acceleration.ts @@ -1,6 +1,6 @@ -import logger from '../logger'; -import { MempoolTransactionExtended } from '../mempool.interfaces'; -import { IEsploraApi } from './bitcoin/esplora-api.interface'; +import logger from '../../logger'; +import { MempoolTransactionExtended } from '../../mempool.interfaces'; +import { IEsploraApi } from '../bitcoin/esplora-api.interface'; const BLOCK_WEIGHT_UNITS = 4_000_000; const BLOCK_SIGOPS = 80_000; diff --git a/backend/src/index.ts b/backend/src/index.ts index f17020cf8..0b2cbb003 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -43,6 +43,7 @@ import redisCache from './api/redis-cache'; import accelerationApi from './api/services/acceleration'; import bitcoinCoreRoutes from './api/bitcoin/bitcoin-core.routes'; import bitcoinSecondClient from './api/bitcoin/bitcoin-second-client'; +import accelerationRoutes from './api/acceleration/acceleration.routes'; import aboutRoutes from './api/about.routes'; class Server { @@ -306,6 +307,9 @@ class Server { nodesRoutes.initRoutes(this.app); channelsRoutes.initRoutes(this.app); } + if (config.MEMPOOL_SERVICES.ACCELERATIONS) { + accelerationRoutes.initRoutes(this.app); + } aboutRoutes.initRoutes(this.app); } diff --git a/backend/src/repositories/AccelerationRepository.ts b/backend/src/repositories/AccelerationRepository.ts index 1c91df050..4969013c4 100644 --- a/backend/src/repositories/AccelerationRepository.ts +++ b/backend/src/repositories/AccelerationRepository.ts @@ -1,4 +1,4 @@ -import { AccelerationInfo, makeBlockTemplate } from '../api/acceleration'; +import { AccelerationInfo, makeBlockTemplate } from '../api/acceleration/acceleration'; import { RowDataPacket } from 'mysql2'; import DB from '../database'; import logger from '../logger'; @@ -7,7 +7,7 @@ import { Common } from '../api/common'; import config from '../config'; import blocks from '../api/blocks'; import accelerationApi, { Acceleration } from '../api/services/acceleration'; -import accelerationCosts from '../api/acceleration'; +import accelerationCosts from '../api/acceleration/acceleration'; import bitcoinApi from '../api/bitcoin/bitcoin-api-factory'; import transactionUtils from '../api/transaction-utils'; import { BlockExtended, MempoolTransactionExtended } from '../mempool.interfaces';