From e875708e1d345d2a26e3515bf66f8e6fe37f9d04 Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Tue, 11 Apr 2023 16:42:16 +0200 Subject: [PATCH 1/2] fix: health/alive handler never errors --- healthx/handler.go | 1 - healthx/openapi/patch.yaml | 6 ------ 2 files changed, 7 deletions(-) diff --git a/healthx/handler.go b/healthx/handler.go index b4f9f57a..04103dd4 100644 --- a/healthx/handler.go +++ b/healthx/handler.go @@ -123,7 +123,6 @@ func (h *Handler) SetVersionRoutes(r router, opts ...Options) { // // Responses: // 200: healthStatus -// 500: genericError func (h *Handler) Alive() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { h.H.Write(rw, r, &swaggerHealthStatus{ diff --git a/healthx/openapi/patch.yaml b/healthx/openapi/patch.yaml index 09b3d693..995a1c1c 100644 --- a/healthx/openapi/patch.yaml +++ b/healthx/openapi/patch.yaml @@ -25,12 +25,6 @@ description: Always "ok". type: string description: {{.ProjectHumanName}} is ready to accept connections. - '500': - content: - application/json: - schema: - "$ref": "#/components/schemas/genericError" - description: genericError summary: Check HTTP Server Status tags: {{ .HealthPathTags | toJson }} - op: replace From 2cfc08f934eb4e75f7b22a5de0dba94b41662c8b Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Wed, 12 Apr 2023 16:41:49 +0200 Subject: [PATCH 2/2] fix: improve health swagger --- healthx/handler.go | 10 ++++++++++ healthx/handler_test.go | 4 ++-- healthx/openapi/patch.yaml | 16 ++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/healthx/handler.go b/healthx/handler.go index 04103dd4..c679cd17 100644 --- a/healthx/handler.go +++ b/healthx/handler.go @@ -120,9 +120,11 @@ func (h *Handler) SetVersionRoutes(r router, opts ...Options) { // // Produces: // - application/json +// - text/plain // // Responses: // 200: healthStatus +// default: unexpectedError func (h *Handler) Alive() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { h.H.Write(rw, r, &swaggerHealthStatus{ @@ -131,6 +133,12 @@ func (h *Handler) Alive() http.Handler { }) } +// swagger:model unexpectedError +// +//nolint:deadcode,unused +//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions +type unexpectedError string + // Ready returns an ok status if the instance is ready to handle HTTP requests and all ReadyCheckers are ok. // // swagger:route GET /health/ready health isInstanceReady @@ -148,10 +156,12 @@ func (h *Handler) Alive() http.Handler { // // Produces: // - application/json +// - text/plain // // Responses: // 200: healthStatus // 503: healthNotReadyStatus +// default: unexpectedError func (h *Handler) Ready(shareErrors bool) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { var notReady = swaggerNotReadyStatus{ diff --git a/healthx/handler_test.go b/healthx/handler_test.go index 4523d593..b3c4eedd 100644 --- a/healthx/handler_test.go +++ b/healthx/handler_test.go @@ -6,7 +6,7 @@ package healthx import ( "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strings" @@ -67,7 +67,7 @@ func TestHealth(t *testing.T) { response, err := c.Get(endpoint) require.NoError(t, err) require.EqualValues(t, http.StatusServiceUnavailable, response.StatusCode) - out, err := ioutil.ReadAll(response.Body) + out, err := io.ReadAll(response.Body) require.NoError(t, err) assert.Equal(t, "{\"error\":{\"code\":500,\"status\":\"Internal Server Error\",\"message\":\"not alive\"}}", strings.TrimSpace(string(out))) return response diff --git a/healthx/openapi/patch.yaml b/healthx/openapi/patch.yaml index 995a1c1c..2d4baef3 100644 --- a/healthx/openapi/patch.yaml +++ b/healthx/openapi/patch.yaml @@ -24,7 +24,13 @@ status: description: Always "ok". type: string - description: {{.ProjectHumanName}} is ready to accept connections. + description: '{{.ProjectHumanName}} is ready to accept connections.' + default: + content: + text/plain: + schema: + type: string + description: Unexpected error summary: Check HTTP Server Status tags: {{ .HealthPathTags | toJson }} - op: replace @@ -53,7 +59,7 @@ status: description: Always "ok". type: string - description: {{.ProjectHumanName}} is ready to accept requests. + description: '{{.ProjectHumanName}} is ready to accept requests.' '503': content: application/json: @@ -68,6 +74,12 @@ type: object type: object description: Ory Kratos is not yet ready to accept requests. + default: + content: + text/plain: + schema: + type: string + description: Unexpected error summary: Check HTTP Server and Database Status tags: {{ .HealthPathTags | toJson }} - op: replace