somratpro commited on
Commit
9038f9e
·
1 Parent(s): 774bd5e

refactor: update dashboard routing logic, improve sync status handling, and enhance UI layout in health-server.js

Browse files
Files changed (1) hide show
  1. health-server.js +53 -26
health-server.js CHANGED
@@ -9,7 +9,6 @@ const GATEWAY_PORT = 7860;
9
  const GATEWAY_HOST = "127.0.0.1";
10
  const startTime = Date.now();
11
  const LLM_MODEL = process.env.LLM_MODEL || "Not Set";
12
- const SPACE_HOST = process.env.SPACE_HOST || "";
13
  const TELEGRAM_ENABLED = !!process.env.TELEGRAM_BOT_TOKEN;
14
  const WHATSAPP_ENABLED = /^true$/i.test(process.env.WHATSAPP_ENABLED || "");
15
  const WHATSAPP_STATUS_FILE = "/tmp/huggingclaw-wa-status.json";
@@ -31,15 +30,18 @@ function parseRequestUrl(url) {
31
  }
32
 
33
  function isDashboardRoute(pathname) {
34
- return pathname === "/" || pathname === DASHBOARD_BASE || pathname === `${DASHBOARD_BASE}/`;
35
- }
36
-
37
- function isDashboardScopedPath(pathname, suffix) {
38
- return pathname === `${DASHBOARD_BASE}/${suffix}`;
39
  }
40
 
41
  function isDashboardAppRoute(pathname) {
42
- return pathname === DASHBOARD_APP_BASE || pathname.startsWith(`${DASHBOARD_APP_BASE}/`);
 
 
 
43
  }
44
 
45
  function isAppRoute(pathname) {
@@ -58,14 +60,13 @@ function isLocalRoute(pathname) {
58
  );
59
  }
60
 
61
- function stripDashboardAppPrefix(path) {
62
- if (path === DASHBOARD_APP_BASE) return "/";
63
  if (path.startsWith(`${DASHBOARD_APP_BASE}/`)) {
64
- return path.slice(DASHBOARD_APP_BASE.length) || "/";
65
  }
66
- if (path === APP_BASE) return "/";
67
- if (path.startsWith(`${APP_BASE}/`)) {
68
- return path.slice(APP_BASE.length) || "/";
69
  }
70
  return path;
71
  }
@@ -144,7 +145,7 @@ function renderSyncBadge(syncData) {
144
  let badgeClass = "status-offline";
145
  let pulseHtml = "";
146
 
147
- if (syncData.status === "success") {
148
  badgeClass = "status-online";
149
  pulseHtml = '<div class="pulse"></div>';
150
  } else if (syncData.status === "syncing") {
@@ -156,7 +157,7 @@ function renderSyncBadge(syncData) {
156
  }
157
 
158
  function renderDashboard(initialData) {
159
- const controlUiHref = `${DASHBOARD_APP_BASE}/`;
160
  return `
161
  <!DOCTYPE html>
162
  <html lang="en">
@@ -334,6 +335,18 @@ function renderDashboard(initialData) {
334
 
335
  #sync-msg { color: var(--text); display: block; margin-top: 4px; }
336
 
 
 
 
 
 
 
 
 
 
 
 
 
337
  .helper-card {
338
  width: 100%;
339
  margin-top: 20px;
@@ -486,6 +499,11 @@ function renderDashboard(initialData) {
486
  padding: 16px;
487
  }
488
 
 
 
 
 
 
489
  .helper-row {
490
  flex-direction: column;
491
  }
@@ -516,18 +534,20 @@ function renderDashboard(initialData) {
516
  </div>
517
  <div class="stat-card">
518
  <span class="stat-label">WhatsApp</span>
519
- <span id="wa-status">${renderChannelBadge(initialData.whatsapp, 'Ready to pair')}</span>
520
  </div>
521
  <div class="stat-card">
522
  <span class="stat-label">Telegram</span>
523
- <span id="tg-status">${renderChannelBadge(initialData.telegram, 'Configured')}</span>
524
  </div>
525
  <a href="${controlUiHref}" id="control-ui-link" class="stat-btn">Open Control UI</a>
526
  </div>
527
 
528
  <div class="stat-card" style="width: 100%;">
529
- <span class="stat-label">Workspace Sync Status</span>
530
- <div id="sync-badge-container">${renderSyncBadge(initialData.sync)}</div>
 
 
531
  <div class="sync-info">
532
  Last Sync Activity: <span id="sync-time">${initialData.sync.timestamp || "Never"}</span>
533
  <span id="sync-msg">${initialData.sync.message || "Waiting for first sync..."}</span>
@@ -612,7 +632,7 @@ function renderDashboard(initialData) {
612
  let badgeClass = 'status-offline';
613
  let pulseHtml = '';
614
 
615
- if (syncData.status === 'success') {
616
  badgeClass = 'status-online';
617
  pulseHtml = '<div class="pulse"></div>';
618
  } else if (syncData.status === 'syncing') {
@@ -886,7 +906,13 @@ function serializeUpgradeHeaders(req, remoteAddress) {
886
  return forwardedHeaders;
887
  }
888
 
889
- function proxyUpgrade(req, socket, head, proxyPath = req.url, proxyPort = GATEWAY_PORT) {
 
 
 
 
 
 
890
  const proxySocket = net.connect(proxyPort, GATEWAY_HOST);
891
 
892
  proxySocket.on("connect", () => {
@@ -958,7 +984,10 @@ const server = http.createServer((req, res) => {
958
  return;
959
  }
960
 
961
- if (pathname === "/uptimerobot/setup" || pathname === DASHBOARD_UPTIMEROBOT_PATH) {
 
 
 
962
  if (req.method !== "POST") {
963
  res.writeHead(405, { "Content-Type": "application/json" });
964
  res.end(JSON.stringify({ message: "Method not allowed" }));
@@ -1018,8 +1047,7 @@ const server = http.createServer((req, res) => {
1018
  }
1019
 
1020
  if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
1021
- const proxyPath =
1022
- stripDashboardAppPrefix(pathname) + (parsedUrl.search || "");
1023
  proxyHttp(req, res, proxyPath, GATEWAY_PORT);
1024
  return;
1025
  }
@@ -1036,8 +1064,7 @@ server.on("upgrade", (req, socket, head) => {
1036
 
1037
  if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
1038
  const parsedUrl = parseRequestUrl(req.url || "/");
1039
- const proxyPath =
1040
- stripDashboardAppPrefix(pathname) + (parsedUrl.search || "");
1041
  proxyUpgrade(req, socket, head, proxyPath, GATEWAY_PORT);
1042
  return;
1043
  }
 
9
  const GATEWAY_HOST = "127.0.0.1";
10
  const startTime = Date.now();
11
  const LLM_MODEL = process.env.LLM_MODEL || "Not Set";
 
12
  const TELEGRAM_ENABLED = !!process.env.TELEGRAM_BOT_TOKEN;
13
  const WHATSAPP_ENABLED = /^true$/i.test(process.env.WHATSAPP_ENABLED || "");
14
  const WHATSAPP_STATUS_FILE = "/tmp/huggingclaw-wa-status.json";
 
30
  }
31
 
32
  function isDashboardRoute(pathname) {
33
+ return (
34
+ pathname === "/" ||
35
+ pathname === DASHBOARD_BASE ||
36
+ pathname === `${DASHBOARD_BASE}/`
37
+ );
38
  }
39
 
40
  function isDashboardAppRoute(pathname) {
41
+ return (
42
+ pathname === DASHBOARD_APP_BASE ||
43
+ pathname.startsWith(`${DASHBOARD_APP_BASE}/`)
44
+ );
45
  }
46
 
47
  function isAppRoute(pathname) {
 
60
  );
61
  }
62
 
63
+ function mapAppProxyPath(path) {
64
+ if (path === DASHBOARD_APP_BASE) return APP_BASE;
65
  if (path.startsWith(`${DASHBOARD_APP_BASE}/`)) {
66
+ return `${APP_BASE}${path.slice(DASHBOARD_APP_BASE.length)}`;
67
  }
68
+ if (path === APP_BASE || path.startsWith(`${APP_BASE}/`)) {
69
+ return path;
 
70
  }
71
  return path;
72
  }
 
145
  let badgeClass = "status-offline";
146
  let pulseHtml = "";
147
 
148
+ if (syncData.status === "success" || syncData.status === "configured") {
149
  badgeClass = "status-online";
150
  pulseHtml = '<div class="pulse"></div>';
151
  } else if (syncData.status === "syncing") {
 
157
  }
158
 
159
  function renderDashboard(initialData) {
160
+ const controlUiHref = `${APP_BASE}/`;
161
  return `
162
  <!DOCTYPE html>
163
  <html lang="en">
 
335
 
336
  #sync-msg { color: var(--text); display: block; margin-top: 4px; }
337
 
338
+ .card-header {
339
+ display: flex;
340
+ align-items: center;
341
+ justify-content: space-between;
342
+ gap: 12px;
343
+ margin-bottom: 8px;
344
+ }
345
+
346
+ .card-header .stat-label {
347
+ margin-bottom: 0;
348
+ }
349
+
350
  .helper-card {
351
  width: 100%;
352
  margin-top: 20px;
 
499
  padding: 16px;
500
  }
501
 
502
+ .card-header {
503
+ align-items: flex-start;
504
+ flex-direction: column;
505
+ }
506
+
507
  .helper-row {
508
  flex-direction: column;
509
  }
 
534
  </div>
535
  <div class="stat-card">
536
  <span class="stat-label">WhatsApp</span>
537
+ <span id="wa-status">${renderChannelBadge(initialData.whatsapp, "Ready to pair")}</span>
538
  </div>
539
  <div class="stat-card">
540
  <span class="stat-label">Telegram</span>
541
+ <span id="tg-status">${renderChannelBadge(initialData.telegram, "Configured")}</span>
542
  </div>
543
  <a href="${controlUiHref}" id="control-ui-link" class="stat-btn">Open Control UI</a>
544
  </div>
545
 
546
  <div class="stat-card" style="width: 100%;">
547
+ <div class="card-header">
548
+ <span class="stat-label">Workspace Sync Status</span>
549
+ <div id="sync-badge-container">${renderSyncBadge(initialData.sync)}</div>
550
+ </div>
551
  <div class="sync-info">
552
  Last Sync Activity: <span id="sync-time">${initialData.sync.timestamp || "Never"}</span>
553
  <span id="sync-msg">${initialData.sync.message || "Waiting for first sync..."}</span>
 
632
  let badgeClass = 'status-offline';
633
  let pulseHtml = '';
634
 
635
+ if (syncData.status === 'success' || syncData.status === 'configured') {
636
  badgeClass = 'status-online';
637
  pulseHtml = '<div class="pulse"></div>';
638
  } else if (syncData.status === 'syncing') {
 
906
  return forwardedHeaders;
907
  }
908
 
909
+ function proxyUpgrade(
910
+ req,
911
+ socket,
912
+ head,
913
+ proxyPath = req.url,
914
+ proxyPort = GATEWAY_PORT,
915
+ ) {
916
  const proxySocket = net.connect(proxyPort, GATEWAY_HOST);
917
 
918
  proxySocket.on("connect", () => {
 
984
  return;
985
  }
986
 
987
+ if (
988
+ pathname === "/uptimerobot/setup" ||
989
+ pathname === DASHBOARD_UPTIMEROBOT_PATH
990
+ ) {
991
  if (req.method !== "POST") {
992
  res.writeHead(405, { "Content-Type": "application/json" });
993
  res.end(JSON.stringify({ message: "Method not allowed" }));
 
1047
  }
1048
 
1049
  if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
1050
+ const proxyPath = mapAppProxyPath(pathname) + (parsedUrl.search || "");
 
1051
  proxyHttp(req, res, proxyPath, GATEWAY_PORT);
1052
  return;
1053
  }
 
1064
 
1065
  if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
1066
  const parsedUrl = parseRequestUrl(req.url || "/");
1067
+ const proxyPath = mapAppProxyPath(pathname) + (parsedUrl.search || "");
 
1068
  proxyUpgrade(req, socket, head, proxyPath, GATEWAY_PORT);
1069
  return;
1070
  }