Skip to content

Commit

Permalink
Merge pull request #565 from Kodylow/0.4.3-hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow authored Oct 21, 2024
2 parents 3d726d9 + 1cd2366 commit 760bad4
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions apps/router/src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const AppContextProvider: React.FC<AppContextProviderProps> = ({
service.baseUrl.startsWith('ws');
const isGateway = (service: Service): service is GatewayConfig =>
service.baseUrl.startsWith('http');

const addService = async (service: Service) => {
const id = await sha256Hash(service.baseUrl);
const newService = { ...service, id };
Expand All @@ -198,7 +199,7 @@ export const AppContextProvider: React.FC<AppContextProviderProps> = ({
payload: { id, gateway: { config: newService as GatewayConfig } },
});
} else {
throw new Error(`Invalid service baseUrl in config.json: ${service}`);
throw new Error(`Invalid service baseUrl: ${service.baseUrl}`);
}
};

Expand All @@ -209,27 +210,65 @@ export const AppContextProvider: React.FC<AppContextProviderProps> = ({
});
};

fetch('/config.json')
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text();
})
.then((text) => {
if (!text.trim()) {
console.warn('Config file is empty');
return;
}
try {
const data = JSON.parse(text);
handleConfig(data);
} catch (error) {
console.error('Error parsing config JSON:', error);
}
})
// Check environment variables
const checkEnvVars = async () => {
if (process.env.REACT_APP_FM_CONFIG_API) {
const id = await sha256Hash(process.env.REACT_APP_FM_CONFIG_API);
console.log(
'Adding guardian config from env var',
process.env.REACT_APP_FM_CONFIG_API
);
await addService({
id,
baseUrl: process.env.REACT_APP_FM_CONFIG_API,
});
}
if (process.env.REACT_APP_FM_GATEWAY_API) {
const id = await sha256Hash(process.env.REACT_APP_FM_GATEWAY_API);
console.log(
'Adding gateway config from env var',
process.env.REACT_APP_FM_GATEWAY_API
);
await addService({
id,
baseUrl: process.env.REACT_APP_FM_GATEWAY_API,
});
}
};

// Fetch config.json
const fetchConfig = () => {
fetch('/config.json')
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text();
})
.then((text) => {
if (!text.trim()) {
console.warn('Config file is empty');
return;
}
try {
const data = JSON.parse(text);
handleConfig(data);
} catch (error) {
console.error('Error parsing config JSON:', error);
}
})
.catch((error) => {
console.error('Error fetching or processing config:', error);
});
};

// Run both checks
checkEnvVars()
.catch((error) => {
console.error('Error fetching or processing config:', error);
console.error('Error in checkEnvVars:', error);
})
.finally(() => {
fetchConfig();
});
}, []);

Expand Down

0 comments on commit 760bad4

Please sign in to comment.