Adding sync external assets feature.
This commit is contained in:
		
							parent
							
								
									4a56fb817e
								
							
						
					
					
						commit
						401ba3e9e7
					
				
							
								
								
									
										6
									
								
								backend/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								backend/.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,7 +1,9 @@
 | 
				
			|||||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
 | 
					# See http://help.github.com/ignore-files/ for more about ignoring files.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# production config
 | 
					# production config and external assets
 | 
				
			||||||
mempool-config.json
 | 
					*.json
 | 
				
			||||||
 | 
					!mempool-config.sample.json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
icons.json
 | 
					icons.json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# compiled output
 | 
					# compiled output
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,8 @@
 | 
				
			|||||||
    "INITIAL_BLOCKS_AMOUNT": 8,
 | 
					    "INITIAL_BLOCKS_AMOUNT": 8,
 | 
				
			||||||
    "MEMPOOL_BLOCKS_AMOUNT": 8,
 | 
					    "MEMPOOL_BLOCKS_AMOUNT": 8,
 | 
				
			||||||
    "PRICE_FEED_UPDATE_INTERVAL": 3600,
 | 
					    "PRICE_FEED_UPDATE_INTERVAL": 3600,
 | 
				
			||||||
    "USE_SECOND_NODE_FOR_MINFEE": false
 | 
					    "USE_SECOND_NODE_FOR_MINFEE": false,
 | 
				
			||||||
 | 
					    "EXTERNAL_ASSETS": []
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "CORE_RPC": {
 | 
					  "CORE_RPC": {
 | 
				
			||||||
    "HOST": "127.0.0.1",
 | 
					    "HOST": "127.0.0.1",
 | 
				
			||||||
 | 
				
			|||||||
@ -6,10 +6,10 @@ class Icons {
 | 
				
			|||||||
  private static FILE_NAME = './icons.json';
 | 
					  private static FILE_NAME = './icons.json';
 | 
				
			||||||
  private iconIds: string[] = [];
 | 
					  private iconIds: string[] = [];
 | 
				
			||||||
  private icons: { [assetId: string]: string; } = {};
 | 
					  private icons: { [assetId: string]: string; } = {};
 | 
				
			||||||
  constructor() {
 | 
					
 | 
				
			||||||
    if (config.MEMPOOL.NETWORK !== 'liquid') {
 | 
					  constructor() {}
 | 
				
			||||||
      return;
 | 
					
 | 
				
			||||||
    }
 | 
					  public loadIcons() {
 | 
				
			||||||
    if (!fs.existsSync(Icons.FILE_NAME)) {
 | 
					    if (!fs.existsSync(Icons.FILE_NAME)) {
 | 
				
			||||||
      logger.warn(`${Icons.FILE_NAME} does not exist. No Liquid icons loaded.`);
 | 
					      logger.warn(`${Icons.FILE_NAME} does not exist. No Liquid icons loaded.`);
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
 | 
				
			|||||||
@ -16,6 +16,7 @@ interface IConfig {
 | 
				
			|||||||
    MEMPOOL_BLOCKS_AMOUNT: number;
 | 
					    MEMPOOL_BLOCKS_AMOUNT: number;
 | 
				
			||||||
    PRICE_FEED_UPDATE_INTERVAL: number;
 | 
					    PRICE_FEED_UPDATE_INTERVAL: number;
 | 
				
			||||||
    USE_SECOND_NODE_FOR_MINFEE: boolean;
 | 
					    USE_SECOND_NODE_FOR_MINFEE: boolean;
 | 
				
			||||||
 | 
					    EXTERNAL_ASSETS: string[];
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
  ESPLORA: {
 | 
					  ESPLORA: {
 | 
				
			||||||
    REST_API_URL: string;
 | 
					    REST_API_URL: string;
 | 
				
			||||||
@ -78,6 +79,7 @@ const defaults: IConfig = {
 | 
				
			|||||||
    'MEMPOOL_BLOCKS_AMOUNT': 8,
 | 
					    'MEMPOOL_BLOCKS_AMOUNT': 8,
 | 
				
			||||||
    'PRICE_FEED_UPDATE_INTERVAL': 3600,
 | 
					    'PRICE_FEED_UPDATE_INTERVAL': 3600,
 | 
				
			||||||
    'USE_SECOND_NODE_FOR_MINFEE': false,
 | 
					    'USE_SECOND_NODE_FOR_MINFEE': false,
 | 
				
			||||||
 | 
					    'EXTERNAL_ASSETS': [],
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  'ESPLORA': {
 | 
					  'ESPLORA': {
 | 
				
			||||||
    'REST_API_URL': 'http://127.0.0.1:3000',
 | 
					    'REST_API_URL': 'http://127.0.0.1:3000',
 | 
				
			||||||
 | 
				
			|||||||
@ -21,6 +21,8 @@ import backendInfo from './api/backend-info';
 | 
				
			|||||||
import loadingIndicators from './api/loading-indicators';
 | 
					import loadingIndicators from './api/loading-indicators';
 | 
				
			||||||
import mempool from './api/mempool';
 | 
					import mempool from './api/mempool';
 | 
				
			||||||
import elementsParser from './api/liquid/elements-parser';
 | 
					import elementsParser from './api/liquid/elements-parser';
 | 
				
			||||||
 | 
					import syncAssets from './sync-assets';
 | 
				
			||||||
 | 
					import icons from './api/liquid/icons';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Server {
 | 
					class Server {
 | 
				
			||||||
  private wss: WebSocket.Server | undefined;
 | 
					  private wss: WebSocket.Server | undefined;
 | 
				
			||||||
@ -77,6 +79,7 @@ class Server {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    this.setUpWebsocketHandling();
 | 
					    this.setUpWebsocketHandling();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await syncAssets.syncAssets();
 | 
				
			||||||
    diskCache.loadMempoolCache();
 | 
					    diskCache.loadMempoolCache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (config.DATABASE.ENABLED) {
 | 
					    if (config.DATABASE.ENABLED) {
 | 
				
			||||||
@ -87,6 +90,10 @@ class Server {
 | 
				
			|||||||
      statistics.startStatistics();
 | 
					      statistics.startStatistics();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (config.MEMPOOL.NETWORK === 'liquid') {
 | 
				
			||||||
 | 
					      icons.loadIcons();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fiatConversion.startService();
 | 
					    fiatConversion.startService();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.setUpHttpApiRoutes();
 | 
					    this.setUpHttpApiRoutes();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										32
									
								
								backend/src/sync-assets.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								backend/src/sync-assets.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					import axios from 'axios';
 | 
				
			||||||
 | 
					import * as fs from 'fs';
 | 
				
			||||||
 | 
					const fsPromises = fs.promises;
 | 
				
			||||||
 | 
					import config from './config';
 | 
				
			||||||
 | 
					import logger from './logger';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const PATH = './';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SyncAssets {
 | 
				
			||||||
 | 
					  constructor() { }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public async syncAssets() {
 | 
				
			||||||
 | 
					    for (const url of config.MEMPOOL.EXTERNAL_ASSETS) {
 | 
				
			||||||
 | 
					      await this.downloadFile(url);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private async downloadFile(url: string) {
 | 
				
			||||||
 | 
					    const fileName = url.split('/').slice(-1)[0];
 | 
				
			||||||
 | 
					    logger.info(`Downloading external asset: ${fileName}...`);
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      const response = await axios.get(url, {
 | 
				
			||||||
 | 
					        responseType: 'stream', timeout: 30000
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					      await fsPromises.writeFile(PATH + fileName, response.data);
 | 
				
			||||||
 | 
					    } catch (e: any) {
 | 
				
			||||||
 | 
					      throw new Error(`Failed to download external asset. ` + e);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default new SyncAssets();
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user