Passing environment via a separate json file (#15)

This commit is contained in:
Pavel 2021-06-22 13:28:17 +03:00 committed by GitHub
parent ba86658c74
commit 01037462d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 7 deletions

4
env/env.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"nodes": [],
"logLevel": "error"
}

View File

@ -24,6 +24,7 @@
gtag('js', new Date()); gtag('js', new Date());
gtag('config', 'G-6ZTQKE1D4L'); gtag('config', 'G-6ZTQKE1D4L');
</script> </script>
<script id="env" type="application/json" src="env/env.json"></script>
<div id="root"></div> <div id="root"></div>
</body> </body>
</html> </html>

View File

@ -19,6 +19,7 @@ import 'css-spinners/dist/all.min.css';
import './main.css'; import './main.css';
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
import log from 'loglevel'; import log from 'loglevel';
import Multiaddr from 'multiaddr';
import { dev, krasnodar } from '@fluencelabs/fluence-network-environment'; import { dev, krasnodar } from '@fluencelabs/fluence-network-environment';
import { import {
createClient, createClient,
@ -33,12 +34,83 @@ import * as serviceWorker from './serviceWorker';
import { eventType } from './types'; import { eventType } from './types';
import { getAll } from './_aqua/app'; import { getAll } from './_aqua/app';
const relayIdx = 3; const defaultNetworkName = 'krasnodar';
const relays = krasnodar; const defaultEnv = {
// const relays = dev; relays: krasnodar,
relayIdx: 3,
logLevel: 'error',
};
function genFlags(peerId) { async function loadScript(script) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', script.src);
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
resolve(xhr.responseText);
}
};
xhr.onerror = reject;
xhr.onabort = reject;
xhr.send();
});
}
function isMultiaddr(multiaddr) {
try {
Multiaddr(multiaddr);
return true;
} catch (error) {
return false;
}
}
async function initEnvironment() {
try {
const script = document.getElementById('env');
if (!script) {
console.log("Couldn't load environment, falling back to default (${defaultNetworkName})");
return defaultEnv;
}
const scriptContent = await loadScript(script);
const envWrapper = JSON.parse(scriptContent);
const res = { ...defaultEnv };
const data = envWrapper ? envWrapper.nodes : [];
if (data.length === 0) {
console.log(`Environment is empty, falling back to default (${defaultNetworkName})`);
} else {
data.forEach((element) => {
if (!element.multiaddr) {
console.error('multiaddr field is missing for ', element);
}
if (!element.peerId) {
console.error('peerId field is missing for ', element);
}
if (!isMultiaddr(element.multiaddr)) {
console.error(`Value ${element.multiaddr} is not a correct multiaddr`);
}
});
res.relays = data;
res.relayIdx = 0;
}
if (envWrapper.logLevel !== undefined) {
res.logLevel = envWrapper.logLevel;
}
return res;
} catch (error) {
console.error("Couldn't parse environment, error: ", error);
}
return defaultEnv;
}
function genFlags(peerId, relays, relayIdx) {
return { return {
peerId, peerId,
relayId: relays[relayIdx].peerId, relayId: relays[relayIdx].peerId,
@ -69,10 +141,10 @@ function event(name, peer, peers, identify, services, modules, blueprints) {
/* eslint-enable */ /* eslint-enable */
(async () => { (async () => {
setLogLevel('INFO'); const { relays, relayIdx, logLevel } = await initEnvironment();
setLogLevel(logLevel);
const pid = await generatePeerId(); const pid = await generatePeerId();
const flags = genFlags(pid.toB58String()); const flags = genFlags(pid.toB58String(), relays, relayIdx);
console.log(`connect with client: ${pid.toB58String()}`); console.log(`connect with client: ${pid.toB58String()}`);
// If the relay is ever changed, an event shall be sent to elm // If the relay is ever changed, an event shall be sent to elm

View File

@ -40,6 +40,9 @@ var common = {
{ {
from: 'favicon.ico', from: 'favicon.ico',
}, },
{
from: './env/*.*',
},
], ],
}), }),
], ],