Z User commited on
Commit
6414e6b
·
1 Parent(s): 300ab73

feat: add /api/diagnostics/gateway-log endpoint for crash diagnostics

Browse files
Files changed (1) hide show
  1. entry.py +45 -0
entry.py CHANGED
@@ -377,6 +377,7 @@ class ProxyHandler(BaseHTTPRequestHandler):
377
  parsed = urlparse(self.path)
378
  path = parsed.path
379
  qs = parsed.query
 
380
 
381
  # ── Dashboard root ──
382
  if path in ("/", "/index.html"):
@@ -386,6 +387,14 @@ class ProxyHandler(BaseHTTPRequestHandler):
386
  if path == "/deploy":
387
  return self._send_html("/app/deploy.html")
388
 
 
 
 
 
 
 
 
 
389
  # ── SSE log stream (handled locally) ──
390
  if path == "/api/logs/stream":
391
  return self._handle_sse()
@@ -558,6 +567,42 @@ class ProxyHandler(BaseHTTPRequestHandler):
558
  if q in _log_subscribers:
559
  _log_subscribers.remove(q)
560
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561
  def _get_log_history(self, query: str = "") -> list:
562
  params = parse_qs(query)
563
  limit = int(params.get("limit", ["100"])[0])
 
377
  parsed = urlparse(self.path)
378
  path = parsed.path
379
  qs = parsed.query
380
+ method_override = "GET"
381
 
382
  # ── Dashboard root ──
383
  if path in ("/", "/index.html"):
 
387
  if path == "/deploy":
388
  return self._send_html("/app/deploy.html")
389
 
390
+ # ── Diagnostic: gateway log dump (no auth required) ──
391
+ if path == "/api/diagnostics/gateway-log":
392
+ return self._handle_gateway_log()
393
+
394
+ # ── Diagnostic: gateway restart ──
395
+ if path == "/api/diagnostics/restart-gateway" and method_override == "POST":
396
+ return self._handle_restart_gateway()
397
+
398
  # ── SSE log stream (handled locally) ──
399
  if path == "/api/logs/stream":
400
  return self._handle_sse()
 
567
  if q in _log_subscribers:
568
  _log_subscribers.remove(q)
569
 
570
+ def _handle_gateway_log(self):
571
+ """Return raw gateway.log contents for diagnostics."""
572
+ log_path = LOG_FILE
573
+ if not os.path.isfile(log_path):
574
+ # Also check the data dir directly
575
+ alt = "/data/hermes/logs/gateway.log"
576
+ if os.path.isfile(alt):
577
+ log_path = alt
578
+
579
+ if not os.path.isfile(log_path):
580
+ self._send_json({"error": "gateway.log not found", "checked": [LOG_FILE, "/data/hermes/logs/gateway.log"]})
581
+ return
582
+
583
+ try:
584
+ with open(log_path, "r", errors="replace") as f:
585
+ content = f.read()
586
+ body = content.encode("utf-8") if content else b"(empty)"
587
+ self.send_response(200)
588
+ self.send_header("Content-Type", "text/plain; charset=utf-8")
589
+ self.send_header("Content-Length", str(len(body)))
590
+ self.send_header("Access-Control-Allow-Origin", "*")
591
+ self.end_headers()
592
+ self.wfile.write(body)
593
+ except Exception as e:
594
+ self._send_json({"error": str(e)})
595
+
596
+ def _handle_restart_gateway(self):
597
+ """Trigger gateway restart via watchdog signal file."""
598
+ try:
599
+ signal_file = "/tmp/hermes-gateway-restart"
600
+ with open(signal_file, "w") as f:
601
+ f.write(str(time.time()))
602
+ self._send_json({"ok": True, "message": "Restart signal sent"})
603
+ except Exception as e:
604
+ self._send_json({"ok": False, "error": str(e)})
605
+
606
  def _get_log_history(self, query: str = "") -> list:
607
  params = parse_qs(query)
608
  limit = int(params.get("limit", ["100"])[0])