From b48cef88e59a343a31cb963efff59f03fb25ed0a Mon Sep 17 00:00:00 2001 From: Toni Tienda Date: Thu, 2 May 2024 13:19:15 +0200 Subject: [PATCH] chore: configure healthchecks in all services --- backend-rest-go/Dockerfile | 3 +++ backend-rest-js/Dockerfile | 3 +++ backend-rest-js/src/index.js | 4 ++++ compose.e2e-cypress.yaml | 3 ++- compose.frontend-htmx-go.yaml | 3 ++- compose.frontend-nextjs.yaml | 4 +++- webapp-htmx-go/Dockerfile | 3 +++ webapp-htmx-go/cmd/main.go | 6 ++++++ webapp-nextjs/Dockerfile | 2 ++ 9 files changed, 28 insertions(+), 3 deletions(-) diff --git a/backend-rest-go/Dockerfile b/backend-rest-go/Dockerfile index ac5cd70..83f6d25 100644 --- a/backend-rest-go/Dockerfile +++ b/backend-rest-go/Dockerfile @@ -16,4 +16,7 @@ COPY --from=builder /app/server . EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=1s --retries=3 CMD wget --tries=1 --spider http://localhost:8080/healthz || exit 1 + CMD ["./server"] \ No newline at end of file diff --git a/backend-rest-js/Dockerfile b/backend-rest-js/Dockerfile index 8923f08..c07e8ec 100644 --- a/backend-rest-js/Dockerfile +++ b/backend-rest-js/Dockerfile @@ -13,4 +13,7 @@ FROM node:18-alpine as production WORKDIR /app COPY --from=build /app/dist/ ./dist/ + +HEALTHCHECK --interval=10s --timeout=1s --retries=3 CMD wget --tries=1 --spider http://localhost:8080/healthz || exit 1 + CMD ["node", "dist/index.js"] \ No newline at end of file diff --git a/backend-rest-js/src/index.js b/backend-rest-js/src/index.js index 3b25299..7dc18f4 100644 --- a/backend-rest-js/src/index.js +++ b/backend-rest-js/src/index.js @@ -11,6 +11,10 @@ app.use((req, res, next) => { next(); }); +app.get("/healthz", (req, res) => { + res.json(`{"message": "OK"}`); +}); + app.use("/v0", v0); v0.use("/tasks", tasks); diff --git a/compose.e2e-cypress.yaml b/compose.e2e-cypress.yaml index 4c02642..e3d959d 100644 --- a/compose.e2e-cypress.yaml +++ b/compose.e2e-cypress.yaml @@ -4,7 +4,8 @@ services: dockerfile: Dockerfile context: e2e-cypress depends_on: - - webapp + webapp: + condition: service_healthy volumes: - ./e2e-cypress/cypress/screenshots:/app/cypress/screenshots - .env:/app/.env diff --git a/compose.frontend-htmx-go.yaml b/compose.frontend-htmx-go.yaml index f961b59..dc05af1 100644 --- a/compose.frontend-htmx-go.yaml +++ b/compose.frontend-htmx-go.yaml @@ -4,7 +4,8 @@ services: context: ./webapp-htmx-go dockerfile: Dockerfile depends_on: - - backend + backend: + condition: service_healthy env_file: - .env environment: diff --git a/compose.frontend-nextjs.yaml b/compose.frontend-nextjs.yaml index 6007730..afe6b9e 100644 --- a/compose.frontend-nextjs.yaml +++ b/compose.frontend-nextjs.yaml @@ -4,8 +4,10 @@ services: dockerfile: Dockerfile context: ./webapp-nextjs depends_on: - - backend + backend: + condition: service_healthy volumes: - .env:/app/.env.local + \ No newline at end of file diff --git a/webapp-htmx-go/Dockerfile b/webapp-htmx-go/Dockerfile index 20d86ff..e456734 100644 --- a/webapp-htmx-go/Dockerfile +++ b/webapp-htmx-go/Dockerfile @@ -17,4 +17,7 @@ COPY --from=builder /app/templates ./templates EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=1s --retries=3 CMD wget --tries=1 --spider http://localhost:8080/healthz || exit 1 + CMD ["./server"] \ No newline at end of file diff --git a/webapp-htmx-go/cmd/main.go b/webapp-htmx-go/cmd/main.go index 22a9ae3..100af8e 100644 --- a/webapp-htmx-go/cmd/main.go +++ b/webapp-htmx-go/cmd/main.go @@ -46,6 +46,12 @@ func main() { return } + http.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { + fmt.Println("GET /healthz") + + w.Write([]byte(`{"message":"OK" }`)) + }) + http.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) { state, err := generateRandomState() diff --git a/webapp-nextjs/Dockerfile b/webapp-nextjs/Dockerfile index d29160a..50537a2 100644 --- a/webapp-nextjs/Dockerfile +++ b/webapp-nextjs/Dockerfile @@ -21,4 +21,6 @@ COPY --from=build /app/.next ./.next EXPOSE 3000 +HEALTHCHECK --interval=10s --timeout=1s --retries=3 CMD wget --tries=1 --spider http://localhost:3000 || exit 1 + CMD ["npm", "start"] \ No newline at end of file