From 484680fc026a3464add79a2cb07ae7eccad50acb Mon Sep 17 00:00:00 2001 From: zhuojie Date: Fri, 24 May 2019 14:35:44 -0700 Subject: [PATCH] Fix health check endpoint --- Makefile | 2 +- docs/api_docs/bundle.yaml | 11 ++++- docs/home.md | 2 +- integration_tests/test.sh | 1 + pkg/handler/handler.go | 5 ++- swagger/health.yaml | 4 +- swagger/index.yaml | 9 +++- swagger_gen/models/health.go | 43 +++++++++++++++++++ swagger_gen/restapi/doc.go | 2 +- swagger_gen/restapi/embedded_spec.go | 30 +++++++++++-- .../operations/health/get_health_responses.go | 26 +++++++++-- 11 files changed, 120 insertions(+), 15 deletions(-) create mode 100644 swagger_gen/models/health.go diff --git a/Makefile b/Makefile index 622ad576..ee7c84a3 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ serve_docs: ################################ api_docs: - @echo "Installing swagger-merger" && yarn global add swagger-merger + @echo "Installing swagger-merger" && npm install swagger-merger -g @swagger-merger -i $(PWD)/swagger/index.yaml -o $(PWD)/docs/api_docs/bundle.yaml checks: diff --git a/docs/api_docs/bundle.yaml b/docs/api_docs/bundle.yaml index 7bbfa42b..fbbf2663 100644 --- a/docs/api_docs/bundle.yaml +++ b/docs/api_docs/bundle.yaml @@ -4,7 +4,7 @@ info: Flagr is a feature flagging, A/B testing and dynamic configuration microservice. The base path for all the APIs is "/api/v1". title: Flagr - version: 1.1.0 + version: 1.1.2 tags: - name: flag description: Everything about the flag @@ -759,7 +759,9 @@ paths: description: Check if Flagr is healthy responses: '200': - description: OK + description: status of health check + schema: + $ref: '#/definitions/health' default: description: generic error response schema: @@ -1218,6 +1220,11 @@ definitions: type: array items: $ref: '#/definitions/evalResult' + health: + type: object + properties: + status: + type: string error: type: object required: diff --git a/docs/home.md b/docs/home.md index b325115d..331dd83b 100644 --- a/docs/home.md +++ b/docs/home.md @@ -37,7 +37,7 @@ Install Dependencies. - Go - Make (for Makefile) -- Yarn (for building UI) +- Yarn and NPM (for building UI) - SQLite3 (for testing) Build from source. diff --git a/integration_tests/test.sh b/integration_tests/test.sh index 40694e3b..7d9f8de9 100644 --- a/integration_tests/test.sh +++ b/integration_tests/test.sh @@ -12,6 +12,7 @@ step_1_test_health() shakedown GET $flagr_url/health status 200 + content_type 'application/json' } step_2_test_crud_flag() diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index f6d1ff31..c7345689 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -3,6 +3,7 @@ package handler import ( "github.com/checkr/flagr/pkg/config" "github.com/checkr/flagr/pkg/entity" + "github.com/checkr/flagr/swagger_gen/models" "github.com/checkr/flagr/swagger_gen/restapi/operations" "github.com/checkr/flagr/swagger_gen/restapi/operations/constraint" "github.com/checkr/flagr/swagger_gen/restapi/operations/distribution" @@ -83,7 +84,9 @@ func setupEvaluation(api *operations.FlagrAPI) { func setupHealth(api *operations.FlagrAPI) { api.HealthGetHealthHandler = health.GetHealthHandlerFunc( - func(health.GetHealthParams) middleware.Responder { return &health.GetHealthOK{} }, + func(health.GetHealthParams) middleware.Responder { + return health.NewGetHealthOK().WithPayload(&models.Health{Status: "OK"}) + }, ) } diff --git a/swagger/health.yaml b/swagger/health.yaml index fa3c1845..0ca22f7e 100644 --- a/swagger/health.yaml +++ b/swagger/health.yaml @@ -5,7 +5,9 @@ get: description: Check if Flagr is healthy responses: 200: - description: OK + description: status of health check + schema: + $ref: "#/definitions/health" default: description: generic error response schema: diff --git a/swagger/index.yaml b/swagger/index.yaml index decd3a31..0a991211 100644 --- a/swagger/index.yaml +++ b/swagger/index.yaml @@ -5,7 +5,7 @@ info: Flagr is a feature flagging, A/B testing and dynamic configuration microservice. The base path for all the APIs is "/api/v1". title: Flagr - version: 1.1.0 + version: 1.1.2 tags: - name: flag description: Everything about the flag @@ -507,6 +507,13 @@ definitions: items: $ref: "#/definitions/evalResult" + # Health check + health: + type: object + properties: + status: + type: string + # Default Error error: type: object diff --git a/swagger_gen/models/health.go b/swagger_gen/models/health.go new file mode 100644 index 00000000..b4511140 --- /dev/null +++ b/swagger_gen/models/health.go @@ -0,0 +1,43 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package models + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + strfmt "github.com/go-openapi/strfmt" + + "github.com/go-openapi/swag" +) + +// Health health +// swagger:model health +type Health struct { + + // status + Status string `json:"status,omitempty"` +} + +// Validate validates this health +func (m *Health) Validate(formats strfmt.Registry) error { + return nil +} + +// MarshalBinary interface implementation +func (m *Health) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *Health) UnmarshalBinary(b []byte) error { + var res Health + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/swagger_gen/restapi/doc.go b/swagger_gen/restapi/doc.go index 03ed931e..4495b380 100644 --- a/swagger_gen/restapi/doc.go +++ b/swagger_gen/restapi/doc.go @@ -10,7 +10,7 @@ Flagr is a feature flagging, A/B testing and dynamic configuration microservice. http Host: localhost BasePath: /api/v1 - Version: 1.1.0 + Version: 1.1.2 Consumes: - application/json diff --git a/swagger_gen/restapi/embedded_spec.go b/swagger_gen/restapi/embedded_spec.go index 1d65a702..ad32dad2 100644 --- a/swagger_gen/restapi/embedded_spec.go +++ b/swagger_gen/restapi/embedded_spec.go @@ -31,7 +31,7 @@ func init() { "info": { "description": "Flagr is a feature flagging, A/B testing and dynamic configuration microservice. The base path for all the APIs is \"/api/v1\".\n", "title": "Flagr", - "version": "1.1.0" + "version": "1.1.2" }, "basePath": "/api/v1", "paths": { @@ -1134,7 +1134,10 @@ func init() { "operationId": "getHealth", "responses": { "200": { - "description": "OK" + "description": "status of health check", + "schema": { + "$ref": "#/definitions/health" + } }, "default": { "description": "generic error response", @@ -1535,6 +1538,14 @@ func init() { } } }, + "health": { + "type": "object", + "properties": { + "status": { + "type": "string" + } + } + }, "putDistributionsRequest": { "type": "object", "required": [ @@ -1793,7 +1804,7 @@ func init() { "info": { "description": "Flagr is a feature flagging, A/B testing and dynamic configuration microservice. The base path for all the APIs is \"/api/v1\".\n", "title": "Flagr", - "version": "1.1.0" + "version": "1.1.2" }, "basePath": "/api/v1", "paths": { @@ -2896,7 +2907,10 @@ func init() { "operationId": "getHealth", "responses": { "200": { - "description": "OK" + "description": "status of health check", + "schema": { + "$ref": "#/definitions/health" + } }, "default": { "description": "generic error response", @@ -3299,6 +3313,14 @@ func init() { } } }, + "health": { + "type": "object", + "properties": { + "status": { + "type": "string" + } + } + }, "putDistributionsRequest": { "type": "object", "required": [ diff --git a/swagger_gen/restapi/operations/health/get_health_responses.go b/swagger_gen/restapi/operations/health/get_health_responses.go index 555532e3..1894fbc7 100644 --- a/swagger_gen/restapi/operations/health/get_health_responses.go +++ b/swagger_gen/restapi/operations/health/get_health_responses.go @@ -16,11 +16,16 @@ import ( // GetHealthOKCode is the HTTP code returned for type GetHealthOK const GetHealthOKCode int = 200 -/*GetHealthOK OK +/*GetHealthOK status of health check swagger:response getHealthOK */ type GetHealthOK struct { + + /* + In: Body + */ + Payload *models.Health `json:"body,omitempty"` } // NewGetHealthOK creates GetHealthOK with default headers values @@ -29,12 +34,27 @@ func NewGetHealthOK() *GetHealthOK { return &GetHealthOK{} } +// WithPayload adds the payload to the get health o k response +func (o *GetHealthOK) WithPayload(payload *models.Health) *GetHealthOK { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the get health o k response +func (o *GetHealthOK) SetPayload(payload *models.Health) { + o.Payload = payload +} + // WriteResponse to the client func (o *GetHealthOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { - rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses - rw.WriteHeader(200) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } } /*GetHealthDefault generic error response