[internal] provide internal rest api to retreive btcusd price history
This commit is contained in:
		
							parent
							
								
									073fe8e8cd
								
							
						
					
					
						commit
						423b41939e
					
				| @ -1,10 +1,15 @@ | |||||||
| import { Application, Request, Response } from 'express'; | import { Application, Request, Response } from 'express'; | ||||||
| import config from '../../config'; | import config from '../../config'; | ||||||
| import pricesUpdater from '../../tasks/price-updater'; | import pricesUpdater from '../../tasks/price-updater'; | ||||||
|  | import logger from '../../logger'; | ||||||
|  | import PricesRepository from '../../repositories/PricesRepository'; | ||||||
| 
 | 
 | ||||||
| class PricesRoutes { | class PricesRoutes { | ||||||
|   public initRoutes(app: Application): void { |   public initRoutes(app: Application): void { | ||||||
|     app.get(config.MEMPOOL.API_URL_PREFIX + 'prices', this.$getCurrentPrices.bind(this)); |     app | ||||||
|  |       .get(config.MEMPOOL.API_URL_PREFIX + 'prices', this.$getCurrentPrices.bind(this)) | ||||||
|  |       .get(config.MEMPOOL.API_URL_PREFIX + 'internal/usd-price-history', this.$getAllPrices.bind(this)) | ||||||
|  |     ; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   private $getCurrentPrices(req: Request, res: Response): void { |   private $getCurrentPrices(req: Request, res: Response): void { | ||||||
| @ -14,6 +19,23 @@ class PricesRoutes { | |||||||
| 
 | 
 | ||||||
|     res.json(pricesUpdater.getLatestPrices()); |     res.json(pricesUpdater.getLatestPrices()); | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   private async $getAllPrices(req: Request, res: Response): Promise<void> { | ||||||
|  |     res.header('Pragma', 'public'); | ||||||
|  |     res.header('Cache-control', 'public'); | ||||||
|  |     res.setHeader('Expires', new Date(Date.now() + 360_0000 / config.MEMPOOL.PRICE_UPDATES_PER_HOUR).toUTCString()); | ||||||
|  | 
 | ||||||
|  |     try { | ||||||
|  |       const usdPriceHistory = await PricesRepository.$getPricesTimesAndId(); | ||||||
|  |       const responseData = usdPriceHistory.map(p => { | ||||||
|  |         return { time: p.time, USD: p.USD }; | ||||||
|  |       }); | ||||||
|  |       res.status(200).json(responseData); | ||||||
|  |     } catch (e: any) { | ||||||
|  |       logger.err(`Exception ${e} in PricesRoutes::$getAllPrices. Code: ${e.code}. Message: ${e.message}`); | ||||||
|  |       res.status(403).send(); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export default new PricesRoutes(); | export default new PricesRoutes(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user