diff --git a/dashboard.py b/dashboard.py index 58b90d9..4c097a3 100644 --- a/dashboard.py +++ b/dashboard.py @@ -52,7 +52,14 @@ class H(BaseHTTPRequestHandler): p=urlparse(self.path).path if p=='/':return self.send(200,INDEX,'text/html; charset=utf-8') if p=='/api/watchlist':return self.send(200,json.dumps(watchlist.read())) - if p=='/api/state':return self.send(200,STATE.read_text() if STATE.exists() else '{}') + if p=='/api/state': + # If someone opens the JSON endpoint directly from a phone/browser, + # send them to the dashboard instead of a raw JSON wall. Programmatic + # callers and dashboard fetch() still receive JSON. + accept=self.headers.get('Accept','') + if 'text/html' in accept and 'application/json' not in accept: + self.send_response(303);self.send_header('Location','/');self.send_header('Cache-Control','no-store');self.end_headers();return + return self.send(200,STATE.read_text() if STATE.exists() else '{}') if p=='/api/report':return self.send(200,report_text(json.loads(STATE.read_text()) if STATE.exists() else {}),'text/plain; charset=utf-8') if p=='/api/log': from collections import deque