Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions src/labnow-open-web/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
StopFilledAlt,
} from "@carbon/icons-react";
import { useSupervisorController } from "./hooks/useSupervisorController";
import { buildHomePath, buildWorkspacePath } from "./utils/runtimeBase";

const PROGRAM_DEFINITIONS = {
caddy: { displayName: "Caddy Server", hidden: true },
Expand Down Expand Up @@ -63,29 +64,6 @@ const PROGRAM_DEFINITIONS = {
},
};

function normalizeBaseUrl(baseUrl) {
if (!baseUrl) {
return "/";
}
return baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
}

function buildPrefixedPath(path) {
if (!path) {
return "";
}
if (/^(https?:)?\/\//.test(path)) {
return path;
}

const runtimeBase =
(typeof window !== "undefined" && window.__LABNOW_URL_PREFIX__) ||
import.meta.env.BASE_URL;
const normalizedBase = normalizeBaseUrl(runtimeBase);
const normalizedPath = String(path).replace(/^\/+/, "");
return `${normalizedBase}${normalizedPath}`;
}

function NotificationBar({ notice, onClose }) {
if (!notice) {
return null;
Expand Down Expand Up @@ -266,7 +244,7 @@ export default function App() {
<HeaderName href="#" prefix="">
<img
className="header-brand-logo"
src={buildPrefixedPath("favicon.svg")}
src={buildHomePath("favicon.svg")}
alt=""
aria-hidden="true"
/>
Expand Down Expand Up @@ -348,7 +326,7 @@ export default function App() {
const running = isRunningState(program.statename);
const programMeta = programMetaByName[program.name];
const programLabel = programMeta?.displayName || program.name;
const programLink = programMeta?.link;
const programLink = buildWorkspacePath(programMeta?.link);
const programLogo = programMeta?.logo || "";
const programDescription = programMeta?.description || "";
const programLinkEnabled =
Expand Down Expand Up @@ -387,7 +365,7 @@ export default function App() {
{programLogo ? (
<img
className="program-logo program-logo-card"
src={buildPrefixedPath(programLogo)}
src={buildHomePath(programLogo)}
alt=""
aria-hidden="true"
/>
Expand Down
22 changes: 3 additions & 19 deletions src/labnow-open-web/src/api/supervisorApi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getRuntimeApiBase } from "../utils/runtimeBase";

function buildJsonHeaders(extraHeaders) {
return {
"Content-Type": "application/json",
Expand Down Expand Up @@ -32,25 +34,7 @@ export async function resolveApiBase() {
return "/api/home";
}

const runtimeBase =
(typeof window !== "undefined" && window.__LABNOW_URL_PREFIX__) ||
import.meta.env.BASE_URL;
if (runtimeBase && typeof window !== "undefined") {
const pathname = new URL(runtimeBase, window.location.href).pathname;
return pathname === "/" ? "/home" : pathname.replace(/\/$/, "");
}

try {
const resp = await fetch(window.location.href, { method: "HEAD" });
const base = resp.headers.get("location-base");
if (base) {
return `${base}home`;
}
} catch (error) {
console.error("Failed to resolve location-base header", error);
}

return "/home";
return typeof window !== "undefined" ? getRuntimeApiBase() : "/home";
}

export function createSupervisorApi(apiBase) {
Expand Down
10 changes: 3 additions & 7 deletions src/labnow-open-web/src/hooks/useSupervisorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import {
createSupervisorApi,
isRunningState,
resolveApiBase,
} from "../api/supervisorApi";
import { getRuntimeApiBase } from "../utils/runtimeBase";

export function useSupervisorController() {
const [loading, setLoading] = useState(false);
const [programs, setPrograms] = useState([]);
const [apiBase, setApiBase] = useState(
import.meta.env.DEV ? "/api/home" : "/home"
const [apiBase] = useState(() =>
import.meta.env.DEV ? "/api/home" : getRuntimeApiBase()
);
const [selectedRowKeys, setSelectedRowKeys] = useState([]);

useEffect(() => {
resolveApiBase().then(setApiBase);
}, []);

const api = useMemo(() => createSupervisorApi(apiBase), [apiBase]);

const patchProgramState = useCallback((name, nextState) => {
Expand Down
43 changes: 43 additions & 0 deletions src/labnow-open-web/src/utils/runtimeBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function isExternalUrl(path) {
return /^(https?:)?\/\//.test(path);
}

export function getRuntimeHomeBasePath() {
const runtimeBase =
(typeof window !== "undefined" && window.__LABNOW_URL_PREFIX__) ||
import.meta.env.BASE_URL ||
"./";

if (typeof window === "undefined") {
return runtimeBase.endsWith("/") ? runtimeBase : `${runtimeBase}/`;
}

const pathname = new URL(runtimeBase, window.location.href).pathname;
return pathname.endsWith("/") ? pathname : `${pathname}/`;
}

export function getRuntimeApiBase() {
const homeBase = getRuntimeHomeBasePath();
return homeBase === "/" ? "/home" : homeBase.replace(/\/$/, "");
}

export function buildHomePath(path) {
if (!path) {
return "";
}
if (isExternalUrl(path)) {
return path;
}
return `${getRuntimeHomeBasePath()}${String(path).replace(/^\/+/, "")}`;
}

export function buildWorkspacePath(path) {
if (!path) {
return "";
}
if (isExternalUrl(path)) {
return path;
}
const prefix = getRuntimeHomeBasePath().replace(/\/home\/?$/, "/");
return `${prefix}${String(path).replace(/^\/+/, "")}`;
}
1 change: 1 addition & 0 deletions tool/cicd/docker-compose.labnow-open.DEV.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
environment:
- PROFILE_LOCALIZE=${PROFILE_LOCALIZE:-aliyun-pub}
- ENV_PROFILE=${ENV_PROFILE:-DEV}
- STATIC_DIR=/opt/labnow-open/web
env_file: ["../credentials/${ENV_PROFILE:-DEV}-labnow-open.env"]
user: "0:0"
ports: ["${PORT_APP:-20000}:80", "${PORT_WEB:-30000}:3000"]
Expand Down
2 changes: 2 additions & 0 deletions tool/cicd/run-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ echo "Action: $ACTION"
echo "Compose: $COMPOSE"
echo "----------------------------------------"

mkdir -pv "$SCRIPT_DIR/../credentials" >/dev/null
touch "$SCRIPT_DIR/../credentials/DEV-labnow-open.env" >/dev/null

case "$ACTION" in
up)
Expand Down