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.
 | 
			
		||||
 | 
			
		||||
# production config
 | 
			
		||||
mempool-config.json
 | 
			
		||||
# production config and external assets
 | 
			
		||||
*.json
 | 
			
		||||
!mempool-config.sample.json
 | 
			
		||||
 | 
			
		||||
icons.json
 | 
			
		||||
 | 
			
		||||
# compiled output
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,8 @@
 | 
			
		||||
    "INITIAL_BLOCKS_AMOUNT": 8,
 | 
			
		||||
    "MEMPOOL_BLOCKS_AMOUNT": 8,
 | 
			
		||||
    "PRICE_FEED_UPDATE_INTERVAL": 3600,
 | 
			
		||||
    "USE_SECOND_NODE_FOR_MINFEE": false
 | 
			
		||||
    "USE_SECOND_NODE_FOR_MINFEE": false,
 | 
			
		||||
    "EXTERNAL_ASSETS": []
 | 
			
		||||
  },
 | 
			
		||||
  "CORE_RPC": {
 | 
			
		||||
    "HOST": "127.0.0.1",
 | 
			
		||||
 | 
			
		||||
@ -6,10 +6,10 @@ class Icons {
 | 
			
		||||
  private static FILE_NAME = './icons.json';
 | 
			
		||||
  private iconIds: string[] = [];
 | 
			
		||||
  private icons: { [assetId: string]: string; } = {};
 | 
			
		||||
  constructor() {
 | 
			
		||||
    if (config.MEMPOOL.NETWORK !== 'liquid') {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  constructor() {}
 | 
			
		||||
 | 
			
		||||
  public loadIcons() {
 | 
			
		||||
    if (!fs.existsSync(Icons.FILE_NAME)) {
 | 
			
		||||
      logger.warn(`${Icons.FILE_NAME} does not exist. No Liquid icons loaded.`);
 | 
			
		||||
      return;
 | 
			
		||||
 | 
			
		||||
@ -16,6 +16,7 @@ interface IConfig {
 | 
			
		||||
    MEMPOOL_BLOCKS_AMOUNT: number;
 | 
			
		||||
    PRICE_FEED_UPDATE_INTERVAL: number;
 | 
			
		||||
    USE_SECOND_NODE_FOR_MINFEE: boolean;
 | 
			
		||||
    EXTERNAL_ASSETS: string[];
 | 
			
		||||
  };
 | 
			
		||||
  ESPLORA: {
 | 
			
		||||
    REST_API_URL: string;
 | 
			
		||||
@ -78,6 +79,7 @@ const defaults: IConfig = {
 | 
			
		||||
    'MEMPOOL_BLOCKS_AMOUNT': 8,
 | 
			
		||||
    'PRICE_FEED_UPDATE_INTERVAL': 3600,
 | 
			
		||||
    'USE_SECOND_NODE_FOR_MINFEE': false,
 | 
			
		||||
    'EXTERNAL_ASSETS': [],
 | 
			
		||||
  },
 | 
			
		||||
  'ESPLORA': {
 | 
			
		||||
    '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 mempool from './api/mempool';
 | 
			
		||||
import elementsParser from './api/liquid/elements-parser';
 | 
			
		||||
import syncAssets from './sync-assets';
 | 
			
		||||
import icons from './api/liquid/icons';
 | 
			
		||||
 | 
			
		||||
class Server {
 | 
			
		||||
  private wss: WebSocket.Server | undefined;
 | 
			
		||||
@ -77,6 +79,7 @@ class Server {
 | 
			
		||||
 | 
			
		||||
    this.setUpWebsocketHandling();
 | 
			
		||||
 | 
			
		||||
    await syncAssets.syncAssets();
 | 
			
		||||
    diskCache.loadMempoolCache();
 | 
			
		||||
 | 
			
		||||
    if (config.DATABASE.ENABLED) {
 | 
			
		||||
@ -87,6 +90,10 @@ class Server {
 | 
			
		||||
      statistics.startStatistics();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (config.MEMPOOL.NETWORK === 'liquid') {
 | 
			
		||||
      icons.loadIcons();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fiatConversion.startService();
 | 
			
		||||
 | 
			
		||||
    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