Skip to content

Commit 673fbfc

Browse files
committed
change&prefix
1 parent 5b69365 commit 673fbfc

File tree

6 files changed

+76
-24
lines changed

6 files changed

+76
-24
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ The frontend was built using:
1515

1616
Github Actions will test, build and upload the frontend to the [labctl repo](https://github.com/labctl/labctl/tree/main/helpers/frontend/html).
1717

18-
# Local testing
18+
## Testing the frontend locally
1919

20-
For local testing you could point it towards another running labctl server instance. This instance is define in the **localhost** variable in Local Storage. You can change this in Web Tools: Application->Local Storage from the default, *tes4:8080*.
20+
When the frontend opens it usually connects to the host and URL on which it was served,- this will not work while testing the frontend on localhost with vite.
2121

22-
When the frontend opens on a http://localhost:* URL it will use the **localhost** variable, else it will connect to the host and URL on which it was served.
22+
For local testing you should point the fontend toward a running labctl server instance. This instance is defined in the **localhost** variable in Local Storage. You can change this in Web Tools: Application->Local Storage from the default, `https://tes4:8080/labctl`.
2323

2424
1. Clone the repo and install deps: `npm install`
2525
2. Start the web server with `npm run dev` or `npx vite`
26-
3. When the frontend runs on http://localhost it will try to connect to a server identified by the value of **localhost** in Local Storage
26+
3. When the frontend runs on <http://localhost> it will try to connect to a server identified by the value of **localhost** in Local Storage
2727

28-
# Local unit tests
28+
## Local unit tests
2929

3030
`npm test` or `npx vitest` for continuous testing during development
31+
32+
## Web API endpoints & Websocket messages
33+
34+
These are documented in the labctl main repo's README file - [labctl/labctl](https://github.com/labctl/labctl)

src/App.vue

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import {
7979
lightTheme,
8080
NAvatar,
8181
} from "naive-ui"
82-
8382
import { useMainStore } from "@/stores/mainStore"
8483
import { useWebSocket } from "@vueuse/core"
8584
import { ws_uri } from "@/utils/const"

src/stores/mainStore.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const useMainStore = defineStore("main", {
2727
/** topo file */
2828
topo: {
2929
name: "",
30+
prefix: undefined as string | undefined,
3031
nodes: {} as Nodes,
3132
links: {} as Links,
3233
/** compiled vars per NE */
@@ -70,6 +71,14 @@ export const useMainStore = defineStore("main", {
7071
} as WsMessage
7172
},
7273

74+
hostName: (state) => (nodeName: string) => {
75+
// https://containerlab.dev/manual/topo-def-file/#prefix
76+
const p: string = state.topo.prefix ?? "clab"
77+
if (p === "") return nodeName
78+
if (p === "__lab-name") return `${state.topo.name}-${nodeName}`
79+
return `${p}-${state.topo.name}-${nodeName}`
80+
},
81+
7382
linkVars: (state) => (linkid: string) => {
7483
const l = state.topo.links[linkid]
7584
if (!l) {
@@ -166,6 +175,11 @@ export const useMainStore = defineStore("main", {
166175
json_fetch(base_uri + "vars").then((resp) => {
167176
Object.assign(this.topo.vars, resp.data)
168177
})
178+
this.fetch_templates()
179+
this.fetch_labfiles()
180+
},
181+
182+
fetch_templates() {
169183
json_fetch(base_uri + "templates")
170184
.then((resp) => {
171185
Object.assign(this.templateFiles, resp.data)
@@ -175,14 +189,17 @@ export const useMainStore = defineStore("main", {
175189
`Could not load templates\n\n${err}\n\nReload the webpage and/or check the server`
176190
)
177191
)
192+
},
193+
194+
fetch_labfiles() {
178195
json_fetch(base_uri + "files")
179196
.then((resp) => {
180197
Object.assign(this.labFiles, resp.data)
181198
})
182199
.catch(() => {
200+
MsgError("Could not load lab files")
183201
this.labFiles = { "readme.md": "could not load lab files" }
184202
})
185-
// }
186203
},
187204

188205
load_config(data?: Array<WsTxResponse>) {
@@ -229,6 +246,11 @@ export const useMainStore = defineStore("main", {
229246
MsgInfo(msg.msg ?? JSON.stringify(msg))
230247
return // don't emit!
231248

249+
case WsMsgCodes.fschange:
250+
console.log(msg)
251+
this.fschange(msg.msg ?? "")
252+
return // don't emit!
253+
232254
default:
233255
console.debug("unknown msg code", msg)
234256
MsgError(`unknown message code\n\n${JSON.stringify(msg)}`, {
@@ -243,6 +265,22 @@ export const useMainStore = defineStore("main", {
243265
// Pass on the message for updates etc
244266
wsRxBus.emit(msg)
245267
},
268+
269+
fschange(name: string) {
270+
if (this.labFiles[name] !== undefined) {
271+
console.log("load lf!", name)
272+
this.fetch_labfiles()
273+
return
274+
}
275+
const np = name.split("/")
276+
const basename = np[np.length - 1]
277+
if (this.templateFiles[basename] !== undefined) {
278+
console.log("load t!", name)
279+
this.fetch_templates()
280+
return
281+
}
282+
console.log("unhandled file change: ", name)
283+
},
246284
},
247285
debounce: {
248286
save: 1500, // debounce save by 300ms

src/utils/const.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
export const base_uri = (() => {
2-
if (typeof window === "undefined") return "/labctl/"
3-
const p = window.location.pathname.split("/", 2)
4-
return `/${p[1] || "labctl"}/`
5-
})()
6-
71
/** If localhost, get localhost+port from local storage, else null */
2+
function lhURL(): URL {
3+
let u = localStorage.getItem("localhost")
4+
if (u === null) {
5+
u = "https://tes4:8080/labctl"
6+
localStorage.setItem("localhost", u)
7+
}
8+
return new URL(u)
9+
}
10+
811
export const localhost = (() => {
912
if (typeof window === "undefined") return ""
10-
const lh = "localhost"
1113
const h = window.location.hostname
12-
if (!(h === lh || h === "127.0.0.1")) {
14+
if (!(h === "localhost" || h === "127.0.0.1")) {
1315
return ""
1416
}
15-
let s = localStorage.getItem(lh)
16-
if (s === null) {
17-
s = "tes4:8080"
18-
localStorage.setItem(lh, s)
19-
}
17+
const url = lhURL()
2018
console.warn(
21-
`Using upstream server ${s}. Change in Dev Tools --> Application --> Local Storage [key=${lh}] & refresh.`
19+
`Using upstream server ${url.host}${url.pathname}. Change in Dev Tools --> Application --> Local Storage [key=localhost] & refresh.`
2220
)
23-
return s
21+
return url.host
22+
})()
23+
24+
export const base_uri = (() => {
25+
if (localhost) {
26+
const url = lhURL()
27+
if (url.pathname && url.pathname !== "/") {
28+
return url.pathname.endsWith("/") ? url.pathname : url.pathname + "/"
29+
}
30+
}
31+
if (typeof window === "undefined") return "/labctl/"
32+
const p = window.location.pathname.split("/", 2)
33+
return `/${p[1] || "labctl"}/`
2434
})()
2535

2636
export const api_uri = (() => {

src/utils/websocket.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export enum WsMsgCodes {
3434
error = "error",
3535
warn = "warn",
3636
info = "info",
37+
fschange = "fschange",
3738
}
3839

3940
export interface WsTxResponse {

src/views/graphView.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
:span="panelWidth[n] ?? 2"
151151
>
152152
<panel-xterm
153-
:target="`clab-${store.topo.name}-${n}`"
153+
:target="store.hostName(n)"
154154
:visible="panelWidth[n] ?? 2"
155155
@update:visible="(v) => sshVis(n, v)"
156156
/>
@@ -382,7 +382,7 @@ function updatelabels() {
382382
}
383383
384384
function toggleVisible(v: number) {
385-
return v === 0 ? 2 : -v
385+
return isNaN(v) || v === 0 ? 2 : -v
386386
}
387387
388388
const sshNodes = ref([] as string[])

0 commit comments

Comments
 (0)