| 307 | |
| 308 | |
| 309 | class Handler(BaseHTTPRequestHandler): |
| 310 | server_version = "ServerStatusManageAPI/1.0" |
| 311 | |
| 312 | def log_message(self, fmt, *args): |
| 313 | print("%s - %s" % (self.address_string(), fmt % args), flush=True) |
| 314 | |
| 315 | def end_headers(self): |
| 316 | self.send_header("Cache-Control", "no-store") |
| 317 | if CORS_ORIGIN: |
| 318 | self.send_header("Access-Control-Allow-Origin", CORS_ORIGIN) |
| 319 | self.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Admin-Token") |
| 320 | self.send_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") |
| 321 | super().end_headers() |
| 322 | |
| 323 | def send_json(self, status, payload): |
| 324 | data = json.dumps(payload, ensure_ascii=False).encode("utf-8") |
| 325 | self.send_response(status) |
| 326 | self.send_header("Content-Type", "application/json; charset=utf-8") |
| 327 | self.send_header("Content-Length", str(len(data))) |
| 328 | self.end_headers() |
| 329 | self.wfile.write(data) |
| 330 | |
| 331 | def send_error_json(self, err): |
| 332 | payload = {"ok": False, "error": err.message} |
| 333 | if err.details is not None: |
| 334 | payload["details"] = err.details |
| 335 | self.send_json(err.status, payload) |
| 336 | |
| 337 | def route(self): |
| 338 | parsed = urlparse(self.path) |
| 339 | path = parsed.path.rstrip("/") or "/" |
| 340 | method = self.command.upper() |
| 341 | if method == "OPTIONS": |
| 342 | self.send_response(204) |
| 343 | self.end_headers() |
| 344 | return |
| 345 | try: |
| 346 | if path == "/api/health" and method == "GET": |
| 347 | pid = get_sergate_pid() |
| 348 | self.send_json(200, { |
| 349 | "ok": True, |
| 350 | "enabled": bool(ADMIN_TOKEN), |
| 351 | "sergate": {"running": bool(pid), "pid": pid}, |
| 352 | "configPath": CONFIG_PATH, |
| 353 | }) |
| 354 | return |
| 355 | if path == "/api/schema" and method == "GET": |
| 356 | self.send_json(200, {"ok": True, "schema": api_schema()}) |
| 357 | return |
| 358 | self.require_auth() |
| 359 | if path == "/api/config": |
| 360 | if method == "GET": |
| 361 | self.send_json(200, {"ok": True, "config": load_config()}) |
| 362 | return |
| 363 | if method == "PUT": |
| 364 | config = read_body(self) |
| 365 | config, pid = write_and_reload(config) |
| 366 | self.send_json(200, {"ok": True, "reloaded": True, "pid": pid, "config": config}) |
nothing calls this directly
no outgoing calls
no test coverage detected