route.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { getDb } from "@/lib/db";
  2. import fs from "fs/promises";
  3. import { withErrorHandling, json } from "@/lib/api/errors";
  4. /**
  5. * GET /api/health
  6. *
  7. * Health check endpoint:
  8. * - Verifies database connectivity.
  9. * - Verifies readability of NAS_ROOT_PATH.
  10. *
  11. * Note:
  12. * - This endpoint returns 200 even if sub-checks fail,
  13. * because it is used to surface partial system state.
  14. * - Unexpected failures (coding bugs, etc.) are still handled by withErrorHandling().
  15. */
  16. export const GET = withErrorHandling(
  17. async function GET() {
  18. const result = {
  19. db: null,
  20. nas: null,
  21. };
  22. // --- Database health ---------------------------------------------------
  23. try {
  24. const db = await getDb();
  25. await db.command({ ping: 1 });
  26. result.db = "ok";
  27. } catch (error) {
  28. // We don't throw here – we report the error in the JSON result
  29. result.db = `error: ${error.message}`;
  30. }
  31. // --- NAS health --------------------------------------------------------
  32. const nasPath = process.env.NAS_ROOT_PATH || "/mnt/niederlassungen";
  33. try {
  34. const entries = await fs.readdir(nasPath);
  35. result.nas = {
  36. path: nasPath,
  37. entriesSample: entries.slice(0, 5),
  38. };
  39. } catch (error) {
  40. // It's okay if NAS is not available in some environments (like local dev),
  41. // we just propagate the error message in the health object.
  42. result.nas = `error: ${error.message}`;
  43. }
  44. return json(result, 200);
  45. },
  46. { logPrefix: "[api/health]" }
  47. );