feat: add multi-server control panel and host-aware sync
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
const SERVER_STORAGE_KEY = "llm_monitor_servers";
|
||||
const ACTIVE_SERVER_KEY = "llm_monitor_active_server";
|
||||
|
||||
function normalizeHost(host) {
|
||||
if (!host) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const trimmed = host.trim();
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return trimmed.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function loadServers() {
|
||||
const raw = localStorage.getItem(SERVER_STORAGE_KEY);
|
||||
if (!raw) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return parsed
|
||||
.map((item) => ({
|
||||
id: String(item.id || ""),
|
||||
name: String(item.name || "").trim(),
|
||||
host: normalizeHost(item.host || "")
|
||||
}))
|
||||
.filter((item) => item.id && item.name && item.host);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function saveServers(servers) {
|
||||
localStorage.setItem(SERVER_STORAGE_KEY, JSON.stringify(servers));
|
||||
}
|
||||
|
||||
function generateServerId() {
|
||||
return `srv_${Date.now()}_${Math.random().toString(16).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
function getActiveServerId() {
|
||||
return localStorage.getItem(ACTIVE_SERVER_KEY);
|
||||
}
|
||||
|
||||
function setActiveServerId(serverId) {
|
||||
localStorage.setItem(ACTIVE_SERVER_KEY, serverId);
|
||||
}
|
||||
|
||||
function getServerById(serverId) {
|
||||
return loadServers().find((server) => server.id === serverId) || null;
|
||||
}
|
||||
|
||||
function getServerIdFromQuery() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
return params.get("server") || "";
|
||||
}
|
||||
|
||||
function getActiveServer() {
|
||||
const queryServerId = getServerIdFromQuery();
|
||||
if (queryServerId) {
|
||||
const fromQuery = getServerById(queryServerId);
|
||||
if (fromQuery) {
|
||||
setActiveServerId(fromQuery.id);
|
||||
return fromQuery;
|
||||
}
|
||||
}
|
||||
|
||||
const activeServerId = getActiveServerId();
|
||||
if (activeServerId) {
|
||||
const activeServer = getServerById(activeServerId);
|
||||
if (activeServer) {
|
||||
return activeServer;
|
||||
}
|
||||
}
|
||||
|
||||
const servers = loadServers();
|
||||
if (servers.length > 0) {
|
||||
setActiveServerId(servers[0].id);
|
||||
return servers[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildServerUrl(path, serverId) {
|
||||
const url = new URL(path, window.location.origin);
|
||||
if (serverId) {
|
||||
url.searchParams.set("server", serverId);
|
||||
}
|
||||
return `${url.pathname}${url.search}`;
|
||||
}
|
||||
Reference in New Issue
Block a user