route.js 1.1 KB

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