Refactor lab 1 for Netron and local confidence views
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
export const COURSEWARE_RUNTIME_CONFIG_PATH = "/courseware-runtime.json";
|
||||
export const LAB1_DEFAULT_NETRON_URL = "http://127.0.0.1:8338";
|
||||
export const LAB3_DEFAULT_TERMINAL_PATH = "/wetty";
|
||||
|
||||
export type CoursewareRuntimeConfig = {
|
||||
lab1NetronUrl?: string;
|
||||
lab3TerminalUrl?: string;
|
||||
};
|
||||
|
||||
export type ResolvedCoursewareRuntimeConfig = {
|
||||
lab1NetronUrl: string;
|
||||
lab3TerminalUrl: string;
|
||||
};
|
||||
|
||||
const loopbackHosts = new Set(["127.0.0.1", "localhost", "::1"]);
|
||||
|
||||
function rewriteLoopbackHost(urlValue: string, currentHostname?: string) {
|
||||
try {
|
||||
const url = new URL(urlValue);
|
||||
if (!currentHostname || !loopbackHosts.has(url.hostname)) {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
url.hostname = currentHostname;
|
||||
return url.toString();
|
||||
} catch {
|
||||
return urlValue;
|
||||
}
|
||||
}
|
||||
|
||||
function getCurrentHostname() {
|
||||
if (typeof window === "undefined") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const hostname = window.location.hostname?.trim();
|
||||
return hostname || undefined;
|
||||
}
|
||||
|
||||
export function getLab1NetronUrl(
|
||||
envValue?: string,
|
||||
currentHostname = getCurrentHostname(),
|
||||
) {
|
||||
const trimmedValue = envValue?.trim();
|
||||
|
||||
if (!trimmedValue) {
|
||||
return rewriteLoopbackHost(LAB1_DEFAULT_NETRON_URL, currentHostname);
|
||||
}
|
||||
|
||||
return rewriteLoopbackHost(trimmedValue, currentHostname);
|
||||
}
|
||||
|
||||
export function getLab3TerminalPath(
|
||||
envValue?: string,
|
||||
currentHostname = getCurrentHostname(),
|
||||
) {
|
||||
const trimmedValue = envValue?.trim();
|
||||
|
||||
if (!trimmedValue) {
|
||||
return LAB3_DEFAULT_TERMINAL_PATH;
|
||||
}
|
||||
|
||||
if (/^https?:\/\//i.test(trimmedValue)) {
|
||||
return rewriteLoopbackHost(trimmedValue, currentHostname);
|
||||
}
|
||||
|
||||
return trimmedValue.startsWith("/") ? trimmedValue : `/${trimmedValue}`;
|
||||
}
|
||||
|
||||
export function normalizeCoursewareRuntimeConfig(
|
||||
config?: CoursewareRuntimeConfig,
|
||||
currentHostname = getCurrentHostname(),
|
||||
): ResolvedCoursewareRuntimeConfig {
|
||||
return {
|
||||
lab1NetronUrl: getLab1NetronUrl(config?.lab1NetronUrl, currentHostname),
|
||||
lab3TerminalUrl: getLab3TerminalPath(
|
||||
config?.lab3TerminalUrl,
|
||||
currentHostname,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchCoursewareRuntimeConfig() {
|
||||
const response = await fetch(COURSEWARE_RUNTIME_CONFIG_PATH, {
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Runtime config request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
const config = (await response.json()) as CoursewareRuntimeConfig;
|
||||
return normalizeCoursewareRuntimeConfig(config);
|
||||
}
|
||||
Reference in New Issue
Block a user