Skip to content

Commit

Permalink
PharmaLedger-IMI/epi-workspace#806 get bdnsHosts from github repo at …
Browse files Browse the repository at this point in the history
…boot
  • Loading branch information
skutner committed Jul 4, 2022
1 parent 4fa9048 commit 31dc234
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions components/bdns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ function BDNS(server) {
const {headersMiddleware} = require('../../utils/middlewares');

let bdnsCache;
const config = require("../../config");
const bdnsConfig = config.getConfig("componentsConfig", "bdns");

async function getBDNSHostsFromURL(url) {
const http = require("opendsu").loadAPI("http");
const bdnsHosts = await http.fetch(url).then(res => res.json());
return bdnsHosts
}

let init_process_runned = false;
async function initialize(){
if(init_process_runned){
return true;

async function initialize() {
if (init_process_runned) {
return true;
}
init_process_runned = true;
const fs = require("fs");
Expand All @@ -29,16 +38,27 @@ function BDNS(server) {

bdnsCache = fs.readFileSync(bdnsHostsPath).toString();

try{
if (bdnsConfig && bdnsConfig.url) {
try {
const bdnsExtensions = await getBDNSHostsFromURL(bdnsConfig.url);
let newRegistry = JSON.parse(bdnsCache);
Object.assign(newRegistry, bdnsExtensions);
bdnsCache = JSON.stringify(newRegistry);
} catch (e) {
console.log(`Failed to get bdns hosts from url`, e);
}
}

try {
console.log("Testing to see if admin component is active and can be used to expand BDNS configuration.");
let adminService = require("./../admin").getAdminService();
let getDomains = $$.promisify(adminService.getDomains);
let domains = await getDomains();
if(domains){
if (domains) {
let bdnsExtensions = {};
for(let i=0; i<domains.length; i++){
for (let i = 0; i < domains.length; i++) {
let domain = domains[i];
if(domain.active){
if (domain.active) {
bdnsExtensions[domain.name] = DOMAIN_TEMPLATE;
}
}
Expand All @@ -47,7 +67,7 @@ function BDNS(server) {
bdnsCache = JSON.stringify(newRegistry);
}
console.log("BDNS configuration was updated accordingly to information retrieved from admin service");
}catch(err){
} catch (err) {
console.info("Admin service not available, skipping the process of loading dynamic configured domains. This is not a problem, it's a configuration.");
}
}
Expand All @@ -64,7 +84,7 @@ function BDNS(server) {
response.setHeader('content-type', 'application/json');
response.statusCode = 200;
response.end(bdnsCache);
}else{
} else {
console.log("Bdns config not available at this moment. A 404 response will be sent.");
response.statusCode = 404;
return response.end('BDNS hosts not found');
Expand Down

0 comments on commit 31dc234

Please # to comment.