diff --git a/frontend/angular.json b/frontend/angular.json index 471cd029c..033c5110d 100644 --- a/frontend/angular.json +++ b/frontend/angular.json @@ -211,7 +211,7 @@ "browserTarget": "mempool:build:production" }, "local": { - "proxyConfig": "proxy.conf.js", + "proxyConfig": "proxy.conf.local.js", "verbose": true }, "staging": { diff --git a/frontend/proxy.conf.local.js b/frontend/proxy.conf.local.js new file mode 100644 index 000000000..ba2248e5a --- /dev/null +++ b/frontend/proxy.conf.local.js @@ -0,0 +1,73 @@ + +const fs = require('fs'); + +let PROXY_CONFIG = require('./proxy.conf.js'); +const BACKEND_CONFIG_FILE_NAME = '../backend/mempool-config.json'; +const FRONTEND_CONFIG_FILE_NAME = 'mempool-frontend-config.json'; + +let backendConfigContent; +let frontendConfigContent; + +// Read frontend config +try { + const rawConfig = fs.readFileSync(FRONTEND_CONFIG_FILE_NAME); + frontendConfigContent = JSON.parse(rawConfig); + console.log(`${FRONTEND_CONFIG_FILE_NAME} file found, using provided config`); +} catch (e) { + console.log(e); + if (e.code !== 'ENOENT') { + throw new Error(e); + } else { + console.log(`${FRONTEND_CONFIG_FILE_NAME} file not found, using default config`); + } +} + +// Read backend config +try { + const rawConfig = fs.readFileSync(BACKEND_CONFIG_FILE_NAME); + backendConfigContent = JSON.parse(rawConfig); + console.log(`${BACKEND_CONFIG_FILE_NAME} file found, using provided config`); +} catch (e) { + console.log(e); + if (e.code !== 'ENOENT') { + throw new Error(e); + } else { + console.log(`${BACKEND_CONFIG_FILE_NAME} file not found, using default config`); + } +} + +// Remove the "/api/**" entry from the default proxy config +let localDevContext = PROXY_CONFIG[0].context + +localDevContext.splice(PROXY_CONFIG[0].context.indexOf('/api/**'), 1); + +PROXY_CONFIG[0].context = localDevContext; + +// Change all targets to localhost +PROXY_CONFIG.map(conf => conf.target = "http://localhost:8999"); + +// Add rules for local backend +if (backendConfigContent) { + PROXY_CONFIG.push({ + context: ['/api/address/**', '/api/tx/**', '/api/block/**', '/api/blocks/**'], + target: `http://localhost:8999`, + secure: false, + changeOrigin: true, + proxyTimeout: 30000, + pathRewrite: { + "^/api/": "/api/v1/" + }, + }); + PROXY_CONFIG.push({ + context: ['/api/v1/**'], + target: `http://localhost:8999`, + secure: false, + changeOrigin: true, + proxyTimeout: 30000 + }); + +} + +console.log(PROXY_CONFIG); + +module.exports = PROXY_CONFIG; \ No newline at end of file