route.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // app/api/health/route.js
  2. import { NextResponse } from "next/server";
  3. import { getDb } from "@/lib/db";
  4. import fs from "fs/promises";
  5. /**
  6. * GET /api/health
  7. *
  8. * Health check endpoint:
  9. * - Verifies database connectivity.
  10. * - Verifies readability of NAS_ROOT_PATH.
  11. */
  12. export async function GET() {
  13. const result = {
  14. db: null,
  15. nas: null,
  16. };
  17. // --- Database health -------------------------------------------------------
  18. try {
  19. const db = await getDb();
  20. await db.command({ ping: 1 });
  21. result.db = "ok";
  22. } catch (error) {
  23. // We don't throw here – we report the error in the JSON result
  24. result.db = `error: ${error.message}`;
  25. }
  26. // --- NAS health ------------------------------------------------------------
  27. const nasPath = process.env.NAS_ROOT_PATH || "/mnt/niederlassungen";
  28. try {
  29. const entries = await fs.readdir(nasPath);
  30. result.nas = {
  31. path: nasPath,
  32. entriesSample: entries.slice(0, 5),
  33. };
  34. } catch (error) {
  35. // It's okay if NAS is not available in some environments (like local dev),
  36. // we just propagate the error message in the health object.
  37. result.nas = `error: ${error.message}`;
  38. }
  39. return NextResponse.json(result);
  40. }