From 5132d177b8ab4e00a7e701e9956222fa2352d42c Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Thu, 27 Apr 2023 22:31:06 +0200 Subject: [PATCH] feat: add healthcheck endpoint --- Dockerfile | 4 +++- frontend/src/pages/api/health.tsx | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 frontend/src/pages/api/health.tsx diff --git a/Dockerfile b/Dockerfile index 3653d0c..4b091c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,7 +31,7 @@ RUN npm run build && npm prune --production # Stage 5: Final image FROM node:19-slim AS runner ENV NODE_ENV=docker -RUN apt-get update && apt-get install -y openssl +RUN apt-get update && apt-get install -y curl openssl WORKDIR /opt/app/frontend COPY --from=frontend-builder /opt/app/public ./public @@ -47,4 +47,6 @@ COPY --from=backend-builder /opt/app/package.json ./ WORKDIR /opt/app EXPOSE 3000 +HEALTHCHECK --interval=10s --timeout=3s CMD curl -f http://localhost:3000/api/health || exit 1 + CMD cp -rn /tmp/img /opt/app/frontend/public && node frontend/server.js & cd backend && npm run prod \ No newline at end of file diff --git a/frontend/src/pages/api/health.tsx b/frontend/src/pages/api/health.tsx new file mode 100644 index 0000000..e015da1 --- /dev/null +++ b/frontend/src/pages/api/health.tsx @@ -0,0 +1,14 @@ +import axios from "axios"; +import { NextApiRequest, NextApiResponse } from "next"; +import getConfig from "next/config"; + +const { apiURL } = getConfig().serverRuntimeConfig; + +export default async (req: NextApiRequest, res: NextApiResponse) => { + const apiStatus = await axios + .get(`${apiURL}/api/configs`) + .then(() => "OK") + .catch(() => "ERROR"); + + res.status(apiStatus == "OK" ? 200 : 500).send(apiStatus); +};