about summary refs log tree commit diff stats
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-26 09:37:34 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-26 09:37:34 +0300
commit9856922b00ac1557f77bba27720cfc31ab73cfaf (patch)
tree8b2ed2876b68c17f012b5ee81e75b06506667dd9 /classes
parent704db45abb32fe2c8f0fa83de67541aa6668c8e8 (diff)
downloadthe-epube-9856922b00ac1557f77bba27720cfc31ab73cfaf.tar.gz
the-epube-9856922b00ac1557f77bba27720cfc31ab73cfaf.tar.xz
add public healthcheck endpoint and fix incorrect path to test data
Diffstat (limited to 'classes')
-rw-r--r--classes/Handler/HealthzHandler.php49
1 files changed, 49 insertions, 0 deletions
diff --git a/classes/Handler/HealthzHandler.php b/classes/Handler/HealthzHandler.php
new file mode 100644
index 0000000..722422c
--- /dev/null
+++ b/classes/Handler/HealthzHandler.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Handler;
+
+use Config;
+use Db;
+use PDO;
+use Request;
+use RequestHandlerInterface;
+use Response;
+
+class HealthzHandler implements RequestHandlerInterface
+{
+    public function handle(Request $request): Response
+    {
+        $status = "ok";
+        $errors = [];
+
+        // Test Calibre SQLite database
+        try {
+            $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB));
+            $caldb->query("SELECT 1 FROM books LIMIT 1");
+        } catch (\PDOException $e) {
+            $status = "degraded";
+            $errors["calibre_db"] = "Connection failed: " . $e->getMessage();
+        }
+
+        // Test main database (via ORM/Db)
+        try {
+            Db::pdo()->query("SELECT 1");
+        } catch (\PDOException $e) {
+            $status = "degraded";
+            $errors["main_db"] = "Connection failed: " . $e->getMessage();
+        }
+
+        $response = (new Response())
+            ->withHeader("Content-Type", "application/json")
+            ->withBody(json_encode([
+                "status" => $status,
+                "errors" => $errors,
+            ]));
+
+        if ($status === "degraded") {
+            $response = $response->withStatus(503);
+        }
+
+        return $response;
+    }
+}