#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
from datetime import datetime

PORT = 8080

class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        if self.path == "/deadmanswitch":
            length = int(self.headers.get("Content-Length", 0))
            body = self.rfile.read(length) if length else b""
            print(f"[{datetime.now()}] TRIGGERED — body: {body!r}")
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b"OK")
        else:
            self.send_response(404)
            self.end_headers()

    def log_message(self, fmt, *args):
        pass  # silence default access log; we print our own above

if __name__ == "__main__":
    server = HTTPServer(("", PORT), Handler)
    print(f"Mock dead man's switch listening on http://localhost:{PORT}/deadmanswitch")
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("\nStopped.")
