Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
fix: add friendly endpoint names, derive default endpoint from hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Jul 10, 2019
1 parent 16164e8 commit c0dfe6e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
57 changes: 39 additions & 18 deletions src/EndpointConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {parse as urlParse, format as urlFormat} from 'url';

const windowProtocol = urlParse(window.location.href).protocol;

const urlMap = {
const endpointUrlMap = {
local:
windowProtocol === 'https:'
? `http://${window.location.hostname}:8443`
Expand All @@ -15,12 +15,41 @@ const urlMap = {
tds: 'https://tds.solana.com:8443',
};

let endpointName = process.env.NODE_ENV === 'development' ? 'local' : 'testnet';
const endpointHostnameMap = {
'edge.testnet.solana.com': 'testnet-edge',
'beta.testnet.solana.com': 'testnet-beta',
'testnet.solana.com': 'testnet',
'tds.solana.com': 'tds',
'explorer.solana.com': 'tds', // Default endpoint for explorer.solana.com
'edge.explorer': 'testnet-edge', // Default endpoint for edge.explorer.solana.com
};

const endpointFriendlyNameMap = {
'testnet-edge': 'Edge Development Testnet',
'testnet-beta': 'Beta Development Testnet',
testnet: 'Public Testnet',
tds: 'Tour de SOL',
local: 'Local Cluster',
};

function getDefaultEndpointName() {
const endpointName = endpointHostnameMap[window.location.hostname];
if (endpointName) {
// Remove 'local' option if not running a local/dev app
delete endpointUrlMap.local;
return endpointName;
}

// Default to the edge testnet when in dev mode.
return 'testnet-edge';
}

let endpointName = getDefaultEndpointName();

export async function load() {
try {
const newEndpointName = await localforage.getItem('endpointName');
if (typeof urlMap[newEndpointName] === 'string') {
if (typeof endpointUrlMap[newEndpointName] === 'string') {
endpointName = newEndpointName;
}
} catch (err) {
Expand All @@ -36,11 +65,13 @@ export function getEndpointName() {
}

export function getEndpoints() {
return Object.keys(urlMap);
return Object.keys(endpointUrlMap).map(name => {
return {name, friendlyName: endpointFriendlyNameMap[name]};
});
}

export function setEndpointName(newEndpointName) {
if (typeof urlMap[newEndpointName] !== 'string') {
if (typeof endpointUrlMap[newEndpointName] !== 'string') {
throw new Error(`Unknown endpoint: ${newEndpointName}`);
}
endpointName = newEndpointName;
Expand All @@ -51,7 +82,7 @@ export function setEndpointName(newEndpointName) {
}

export function getRpcUrl() {
return urlMap[endpointName];
return endpointUrlMap[endpointName];
}

export function getApiUrl() {
Expand All @@ -63,7 +94,6 @@ export function getApiUrl() {
urlParts.port = '3001';
}
const url = urlFormat(urlParts);
console.info('getApiUrl:', url);
return url;
}

Expand All @@ -76,7 +106,6 @@ export function getApiWebsocketUrl() {
urlParts.protocol = 'ws:';
}
const url = urlFormat(urlParts);
console.info('getApiWebsocketUrl:', url);
return url;
}

Expand All @@ -86,27 +115,19 @@ export function getMetricsDashboardUrl() {
if (endpointName === 'local') {
matches = window.location.hostname.match('(.*).solana.com');
} else {
const endpointUrl = urlMap[endpointName];
const endpointUrl = endpointUrlMap[endpointName];
matches = endpointUrl.match('/([^/]*).solana.com');
}

let url =
'https://metrics.solana.com:3000/d/testnet-beta/testnet-monitor-beta?refresh=5s&from=now-5m&to=now';
if (matches) {
const metricsDbMap = {
'edge.testnet': 'testnet-edge',
'beta.testnet': 'testnet-beta',
'testnet': 'testnet',
'tds': 'testnet-tds',
};
console.log('getMetricsDashboardUrl matches:', matches);

const testnet = metricsDbMap[matches[1]];
const testnet = endpointHostnameMap[matches[1]];
console.log('getMetricsDashboardUrl testnet:', testnet);
if (testnet) {
url += `&var-testnet=${testnet}`;
}
}
console.log('getMetricsDashboardUrl:', url);
return url;
}
6 changes: 3 additions & 3 deletions src/v1/BxAppBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ class BxAppBar extends React.Component {
onChange={this.handleSetEndpointName}
native
>
{EndpointConfig.getEndpoints().map((name, index) => {
{EndpointConfig.getEndpoints().map((endpoint, index) => {
return (
<option key={index} value={name}>
{name}
<option key={index} value={endpoint.name}>
{endpoint.friendlyName}
</option>
);
})}
Expand Down
4 changes: 2 additions & 2 deletions src/v2/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const Header = () => {
};
const endPointsList = EndpointConfig.getEndpoints();
const renderEndpointOption = endpoint => (
<option key={endpoint} value={endpoint}>
{endpoint}
<option key={endpoint.name} value={endpoint.name}>
{endpoint.friendlyName}
</option>
);

Expand Down

0 comments on commit c0dfe6e

Please sign in to comment.