Supports customizing the API server address of the Web frontend through the --api-host parameter (#913)

This commit is contained in:
Mg Pig
2025-06-02 06:46:12 +08:00
committed by GitHub
parent 0a38a8ef4a
commit b469f8197a
7 changed files with 150 additions and 55 deletions

View File

@@ -1,10 +1,17 @@
const defaultApiHost = 'https://config-server.easytier.cn';
interface ApiHost {
value: string;
usedAt: number;
}
let apiMeta: {
api_host: string;
} | undefined = (window as any).apiMeta;
// remove trailing slashes from the URL
const cleanUrl = (url: string) => url.replace(/\/+$/, '');
const defaultApiHost = cleanUrl(apiMeta?.api_host ?? `${location.origin}${location.pathname}`);
const isValidHttpUrl = (s: string): boolean => {
let url;
@@ -45,7 +52,7 @@ const saveApiHost = (host: string) => {
}
let hosts = cleanAndLoadApiHosts();
const newHost: ApiHost = {value: host, usedAt: Date.now()};
const newHost: ApiHost = { value: host, usedAt: Date.now() };
hosts = hosts.filter((h) => h.value !== host);
hosts.push(newHost);
localStorage.setItem('apiHosts', JSON.stringify(hosts));
@@ -61,4 +68,4 @@ const getInitialApiHost = (): string => {
}
};
export {getInitialApiHost, cleanAndLoadApiHosts, saveApiHost}
export { getInitialApiHost, cleanAndLoadApiHosts, saveApiHost }