update webhook service dan API untuk webhook control web dapat status json sekarang

This commit is contained in:
Alberto-Audrix committed 2026-08-14 16:29:58 +07:00
1 parent c80984ba80
commit b52e876eed
3 files changed
+90 -18

No files matched your search

+30 -7
View File
@@ -2,7 +2,9 @@
"""
Standalone status webhook (stdlib only, HTTP + HTTPS).
Receives GET /?lokasi=IN|OUT&status=ON|OFF.
Receives:
GET /status -> {"status": "IN"|"OUT"|"OFF"}
GET /?lokasi=IN|OUT&status=ON|OFF -> success | fail
State stored in JSON as IN | OUT | OFF.
@@ -19,6 +21,7 @@ from __future__ import annotations
import json
import os
import signal
import ssl
import subprocess
import threading
@@ -144,7 +147,12 @@ class StatusHandler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
parsed = urlparse(self.path)
if parsed.path not in ("/", ""):
path = parsed.path.rstrip("/") or "/"
if path == "/status":
current = load_status() or "OFF"
self._send_json(200, {"status": current})
return
if path not in ("/", ""):
self._send_text(404, "Not Found")
return
@@ -184,6 +192,14 @@ class StatusHandler(BaseHTTPRequestHandler):
self.end_headers()
self.wfile.write(raw)
def _send_json(self, code: int, payload: dict) -> None:
raw = json.dumps(payload).encode("utf-8")
self.send_response(code)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(raw)))
self.end_headers()
self.wfile.write(raw)
def make_http_server(port: int = HTTP_PORT) -> ThreadingHTTPServer:
return ThreadingHTTPServer(("0.0.0.0", port), StatusHandler)
@@ -256,13 +272,20 @@ def main() -> None:
if http_server is None and https_server is None:
raise SystemExit("status_webhook: neither HTTP nor HTTPS could bind")
stop = threading.Event()
def _handle_stop(_signum, _frame) -> None:
stop.set()
signal.signal(signal.SIGTERM, _handle_stop)
signal.signal(signal.SIGINT, _handle_stop)
try:
# serve_forever already running on threads; park main until interrupt.
while True:
threading.Event().wait(3600)
except KeyboardInterrupt:
print("\nstatus_webhook stopped")
# serve_forever already running on threads; park main until stop.
while not stop.is_set():
stop.wait(3600)
finally:
print("status_webhook stopped")
stop_servers(http_server, https_server)