route.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { getDb } from "@/lib/db";
  2. import fs from "fs/promises";
  3. import { withErrorHandling, json } from "@/lib/api/errors";
  4. /**
  5. * Next.js Route Handler caching configuration (RHL-006):
  6. *
  7. * We force this route to execute dynamically on every request.
  8. *
  9. * Reasons:
  10. * - NAS contents can change at any time (new scans).
  11. * - Auth/RBAC-protected responses must not be cached/shared across users.
  12. * - We rely on a small storage-layer TTL micro-cache instead of Next route caching.
  13. */
  14. export const dynamic = "force-dynamic";
  15. /**
  16. * GET /api/health
  17. *
  18. * Health check endpoint:
  19. * - Verifies database connectivity.
  20. * - Verifies readability of NAS_ROOT_PATH.
  21. *
  22. * Note:
  23. * - This endpoint returns 200 even if sub-checks fail,
  24. * because it is used to surface partial system state.
  25. * - Unexpected failures (coding bugs, etc.) are still handled by withErrorHandling().
  26. */
  27. export const GET = withErrorHandling(
  28. async function GET() {
  29. const result = {
  30. db: null,
  31. nas: null,
  32. };
  33. // --- Database health ---------------------------------------------------
  34. try {
  35. const db = await getDb();
  36. await db.command({ ping: 1 });
  37. result.db = "ok";
  38. } catch (error) {
  39. // We don't throw here – we report the error in the JSON result
  40. result.db = `error: ${error.message}`;
  41. }
  42. // --- NAS health --------------------------------------------------------
  43. const nasPath = process.env.NAS_ROOT_PATH || "/mnt/niederlassungen";
  44. try {
  45. const entries = await fs.readdir(nasPath);
  46. result.nas = {
  47. path: nasPath,
  48. entriesSample: entries.slice(0, 5),
  49. };
  50. } catch (error) {
  51. // It's okay if NAS is not available in some environments (like local dev),
  52. // we just propagate the error message in the health object.
  53. result.nas = `error: ${error.message}`;
  54. }
  55. return json(result, 200);
  56. },
  57. { logPrefix: "[api/health]" }
  58. );