Merge pull request #4879 from mempool/nymkappa/proxy-accel-endpoints
[accelerator] proxy acceleration api to prod
This commit is contained in:
		
						commit
						c14e8797e2
					
				
							
								
								
									
										75
									
								
								backend/src/api/acceleration/acceleration.routes.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								backend/src/api/acceleration/acceleration.routes.ts
									
									
									
									
									
										Normal file
									
								
							| @ -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(); | ||||||
| @ -1,6 +1,6 @@ | |||||||
| import logger from '../logger'; | import logger from '../../logger'; | ||||||
| import { MempoolTransactionExtended } from '../mempool.interfaces'; | import { MempoolTransactionExtended } from '../../mempool.interfaces'; | ||||||
| import { IEsploraApi } from './bitcoin/esplora-api.interface'; | import { IEsploraApi } from '../bitcoin/esplora-api.interface'; | ||||||
| 
 | 
 | ||||||
| const BLOCK_WEIGHT_UNITS = 4_000_000; | const BLOCK_WEIGHT_UNITS = 4_000_000; | ||||||
| const BLOCK_SIGOPS = 80_000; | const BLOCK_SIGOPS = 80_000; | ||||||
| @ -43,6 +43,7 @@ import redisCache from './api/redis-cache'; | |||||||
| import accelerationApi from './api/services/acceleration'; | import accelerationApi from './api/services/acceleration'; | ||||||
| import bitcoinCoreRoutes from './api/bitcoin/bitcoin-core.routes'; | import bitcoinCoreRoutes from './api/bitcoin/bitcoin-core.routes'; | ||||||
| import bitcoinSecondClient from './api/bitcoin/bitcoin-second-client'; | import bitcoinSecondClient from './api/bitcoin/bitcoin-second-client'; | ||||||
|  | import accelerationRoutes from './api/acceleration/acceleration.routes'; | ||||||
| import aboutRoutes from './api/about.routes'; | import aboutRoutes from './api/about.routes'; | ||||||
| 
 | 
 | ||||||
| class Server { | class Server { | ||||||
| @ -306,6 +307,9 @@ class Server { | |||||||
|       nodesRoutes.initRoutes(this.app); |       nodesRoutes.initRoutes(this.app); | ||||||
|       channelsRoutes.initRoutes(this.app); |       channelsRoutes.initRoutes(this.app); | ||||||
|     } |     } | ||||||
|  |     if (config.MEMPOOL_SERVICES.ACCELERATIONS) { | ||||||
|  |       accelerationRoutes.initRoutes(this.app); | ||||||
|  |     } | ||||||
|     aboutRoutes.initRoutes(this.app); |     aboutRoutes.initRoutes(this.app); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| import { AccelerationInfo, makeBlockTemplate } from '../api/acceleration'; | import { AccelerationInfo, makeBlockTemplate } from '../api/acceleration/acceleration'; | ||||||
| import { RowDataPacket } from 'mysql2'; | import { RowDataPacket } from 'mysql2'; | ||||||
| import DB from '../database'; | import DB from '../database'; | ||||||
| import logger from '../logger'; | import logger from '../logger'; | ||||||
| @ -7,7 +7,7 @@ import { Common } from '../api/common'; | |||||||
| import config from '../config'; | import config from '../config'; | ||||||
| import blocks from '../api/blocks'; | import blocks from '../api/blocks'; | ||||||
| import accelerationApi, { Acceleration } from '../api/services/acceleration'; | 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 bitcoinApi from '../api/bitcoin/bitcoin-api-factory'; | ||||||
| import transactionUtils from '../api/transaction-utils'; | import transactionUtils from '../api/transaction-utils'; | ||||||
| import { BlockExtended, MempoolTransactionExtended } from '../mempool.interfaces'; | import { BlockExtended, MempoolTransactionExtended } from '../mempool.interfaces'; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user