somratpro commited on
Commit
411ffb9
·
1 Parent(s): 85665cf

feat: add support for HF backup configuration and update routing to handle app-based sync status reporting

Browse files
Files changed (2) hide show
  1. health-server.js +21 -3
  2. workspace-sync.py +6 -0
health-server.js CHANGED
@@ -13,11 +13,14 @@ 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";
 
 
16
  const DASHBOARD_BASE = "/dashboard";
17
  const DASHBOARD_STATUS_PATH = `${DASHBOARD_BASE}/status`;
18
  const DASHBOARD_HEALTH_PATH = `${DASHBOARD_BASE}/health`;
19
  const DASHBOARD_UPTIMEROBOT_PATH = `${DASHBOARD_BASE}/uptimerobot/setup`;
20
  const DASHBOARD_APP_BASE = `${DASHBOARD_BASE}/app`;
 
21
 
22
  function parseRequestUrl(url) {
23
  try {
@@ -39,6 +42,10 @@ function isDashboardAppRoute(pathname) {
39
  return pathname === DASHBOARD_APP_BASE || pathname.startsWith(`${DASHBOARD_APP_BASE}/`);
40
  }
41
 
 
 
 
 
42
  function isLocalRoute(pathname) {
43
  return (
44
  pathname === "/health" ||
@@ -56,6 +63,10 @@ function stripDashboardAppPrefix(path) {
56
  if (path.startsWith(`${DASHBOARD_APP_BASE}/`)) {
57
  return path.slice(DASHBOARD_APP_BASE.length) || "/";
58
  }
 
 
 
 
59
  return path;
60
  }
61
 
@@ -86,6 +97,12 @@ function readSyncStatus() {
86
  return JSON.parse(fs.readFileSync("/tmp/sync-status.json", "utf8"));
87
  }
88
  } catch {}
 
 
 
 
 
 
89
  return { status: "unknown", message: "No sync data yet" };
90
  }
91
 
@@ -139,7 +156,7 @@ function renderSyncBadge(syncData) {
139
  }
140
 
141
  function renderDashboard(initialData) {
142
- const controlUiHref = SPACE_HOST ? `https://${SPACE_HOST}` : `${DASHBOARD_APP_BASE}/`;
143
  return `
144
  <!DOCTYPE html>
145
  <html lang="en">
@@ -687,6 +704,7 @@ function renderDashboard(initialData) {
687
  updateStats();
688
  setInterval(updateStats, 10000);
689
  restoreMonitorUiState();
 
690
  document.getElementById('uptimerobot-btn').addEventListener('click', setupUptimeRobot);
691
  document.getElementById('uptimerobot-toggle').addEventListener('click', toggleMonitorSetup);
692
  </script>
@@ -993,7 +1011,7 @@ const server = http.createServer((req, res) => {
993
  return;
994
  }
995
 
996
- if (isDashboardAppRoute(pathname)) {
997
  const proxyPath =
998
  stripDashboardAppPrefix(pathname) + (parsedUrl.search || "");
999
  proxyHttp(req, res, proxyPath);
@@ -1010,7 +1028,7 @@ server.on("upgrade", (req, socket, head) => {
1010
  return;
1011
  }
1012
 
1013
- if (isDashboardAppRoute(pathname)) {
1014
  const parsedUrl = parseRequestUrl(req.url || "/");
1015
  const proxyPath =
1016
  stripDashboardAppPrefix(pathname) + (parsedUrl.search || "");
 
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";
16
+ const HF_BACKUP_ENABLED = !!(process.env.HF_USERNAME && process.env.HF_TOKEN);
17
+ const SYNC_INTERVAL = process.env.SYNC_INTERVAL || "600";
18
  const DASHBOARD_BASE = "/dashboard";
19
  const DASHBOARD_STATUS_PATH = `${DASHBOARD_BASE}/status`;
20
  const DASHBOARD_HEALTH_PATH = `${DASHBOARD_BASE}/health`;
21
  const DASHBOARD_UPTIMEROBOT_PATH = `${DASHBOARD_BASE}/uptimerobot/setup`;
22
  const DASHBOARD_APP_BASE = `${DASHBOARD_BASE}/app`;
23
+ const APP_BASE = "/app";
24
 
25
  function parseRequestUrl(url) {
26
  try {
 
42
  return pathname === DASHBOARD_APP_BASE || pathname.startsWith(`${DASHBOARD_APP_BASE}/`);
43
  }
44
 
45
+ function isAppRoute(pathname) {
46
+ return pathname === APP_BASE || pathname.startsWith(`${APP_BASE}/`);
47
+ }
48
+
49
  function isLocalRoute(pathname) {
50
  return (
51
  pathname === "/health" ||
 
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
  }
72
 
 
97
  return JSON.parse(fs.readFileSync("/tmp/sync-status.json", "utf8"));
98
  }
99
  } catch {}
100
+ if (HF_BACKUP_ENABLED) {
101
+ return {
102
+ status: "configured",
103
+ message: `Backup is enabled. Waiting for the next sync window (${SYNC_INTERVAL}s).`,
104
+ };
105
+ }
106
  return { status: "unknown", message: "No sync data yet" };
107
  }
108
 
 
156
  }
157
 
158
  function renderDashboard(initialData) {
159
+ const controlUiHref = `${DASHBOARD_APP_BASE}/`;
160
  return `
161
  <!DOCTYPE html>
162
  <html lang="en">
 
704
  updateStats();
705
  setInterval(updateStats, 10000);
706
  restoreMonitorUiState();
707
+ document.getElementById('control-ui-link').setAttribute('href', getDashboardBase() + '/app/');
708
  document.getElementById('uptimerobot-btn').addEventListener('click', setupUptimeRobot);
709
  document.getElementById('uptimerobot-toggle').addEventListener('click', toggleMonitorSetup);
710
  </script>
 
1011
  return;
1012
  }
1013
 
1014
+ if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
1015
  const proxyPath =
1016
  stripDashboardAppPrefix(pathname) + (parsedUrl.search || "");
1017
  proxyHttp(req, res, proxyPath);
 
1028
  return;
1029
  }
1030
 
1031
+ if (isDashboardAppRoute(pathname) || isAppRoute(pathname)) {
1032
  const parsedUrl = parseRequestUrl(req.url || "/");
1033
  const proxyPath =
1034
  stripDashboardAppPrefix(pathname) + (parsedUrl.search || "");
workspace-sync.py CHANGED
@@ -195,6 +195,12 @@ def main():
195
  print("📁 Workspace sync: no git repo and no HF credentials, skipping.")
196
  return
197
 
 
 
 
 
 
 
198
  # Give the gateway a short head start before the first sync probe.
199
  time.sleep(INITIAL_DELAY)
200
 
 
195
  print("📁 Workspace sync: no git repo and no HF credentials, skipping.")
196
  return
197
 
198
+ # Give the gateway a short head start before the first sync probe.
199
+ if use_hf_hub:
200
+ write_sync_status("configured", f"Backup enabled. Waiting for next sync in {INTERVAL}s.")
201
+ else:
202
+ write_sync_status("configured", f"Git sync enabled. Waiting for next sync in {INTERVAL}s.")
203
+
204
  # Give the gateway a short head start before the first sync probe.
205
  time.sleep(INITIAL_DELAY)
206