Skip to content

Commit 82fdef3

Browse files
authored
dashboard: allow changing the dashboard api url using an env var (#3892)
1 parent 4c46d56 commit 82fdef3

File tree

6 files changed

+57
-13
lines changed

6 files changed

+57
-13
lines changed

dashboard/next.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
33
output: "standalone",
4-
basePath: "/ray"
54
};
65

76
export default nextConfig;

dashboard/src/app/api/config/route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server';
2+
3+
export async function GET() {
4+
const config = {
5+
apiUrl: process.env.NEXT_PUBLIC_API_URL || process.env.API_URL || "http://localhost:31888/apis/v1",
6+
};
7+
8+
return NextResponse.json(config);
9+
}

dashboard/src/app/page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ import {
1313
import NextLink from "next/link";
1414
import WorkIcon from "@mui/icons-material/Work";
1515
import LanIcon from "@mui/icons-material/Lan";
16-
import { useEffect, useState } from "react";
16+
import { useEffect } from "react";
1717
import { useRouter } from "next/navigation";
1818
import { useFirstVisit } from "@/components/FirstVisitContext";
1919
import { roblox } from "@/utils/constants";
20+
import { fetchRuntimeConfig } from "@/utils/constants";
21+
2022
const HomePage = () => {
2123
const router = useRouter();
2224
const { firstVisit } = useFirstVisit();
25+
2326
useEffect(() => {
27+
fetchRuntimeConfig();
28+
2429
if (firstVisit) {
2530
router.push("/jobs");
2631
}
2732
}, [])
33+
2834
return (
2935
<>
3036
<Box

dashboard/src/hooks/api/useCreateJob.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { useSnackBar } from "@/components/SnackBarProvider";
44
import { useNamespace } from "@/components/NamespaceProvider";
55
import { useRouter } from "next/navigation";
66
import { config } from "@/utils/constants";
7-
import { Job } from "@/types/rayjob";
87

98
// TODO: still hard-coded
109
async function _createJob(

dashboard/src/utils/constants.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
11
export const ALL_NAMESPACES = "all"
2-
// I'm developing in the stage cluster, so I'm directly using the deployed backend
3-
const development = {
4-
url: "http://localhost:31888/apis/v1",
5-
};
62

7-
const production = {
3+
interface RuntimeConfig {
4+
url: string;
5+
}
6+
7+
const defaultConfig: RuntimeConfig = {
88
url: "http://localhost:31888/apis/v1",
99
};
1010

11-
export const config =
12-
process.env.NODE_ENV === "development" ? development : production;
11+
let runtimeConfig: RuntimeConfig | null = null;
12+
13+
export async function fetchRuntimeConfig(): Promise<RuntimeConfig> {
14+
if (runtimeConfig) {
15+
return runtimeConfig;
16+
}
17+
18+
try {
19+
const response = await fetch('/api/config');
20+
if (response.ok) {
21+
const data = await response.json();
22+
runtimeConfig = {
23+
url: data.apiUrl || defaultConfig.url,
24+
};
25+
return runtimeConfig;
26+
}
27+
} catch (error) {
28+
console.warn('Failed to fetch runtime config, using default:', error);
29+
}
30+
31+
// Fallback to default config
32+
runtimeConfig = defaultConfig;
33+
return runtimeConfig;
34+
}
35+
36+
export const config = {
37+
async getUrl(): Promise<string> {
38+
const cfg = await fetchRuntimeConfig();
39+
return cfg.url;
40+
},
41+
42+
get url(): string {
43+
return runtimeConfig?.url || defaultConfig.url;
44+
}
45+
};
1346

1447
export const roblox = false;

dashboard/src/utils/fetch.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export default async function fetcher(
1616
endpoint: string,
1717
...args: RequestInit[]
1818
) {
19-
const baseUrl = config.url;
20-
console.log(`${baseUrl}${endpoint}`);
21-
// await new Promise((resolve) => setTimeout(resolve, 10000));
19+
const baseUrl = await config.getUrl();
2220
const res = await fetch(`${baseUrl}${endpoint}`, ...args);
2321
if (!res.ok) {
2422
const error = new FetchError("An error occurred while fetching the data");

0 commit comments

Comments
 (0)