Skip to content

Commit

Permalink
refactor: add strict response schema for HealthCheck handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Nov 4, 2020
1 parent 847a843 commit d72be8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const (
ErrResourceNotFound = "resource not found"
)

type HealthCheckResponse struct {
Tag string `json:"tag"`
Commit string `json:"commit"`
}

// @title Gilfoyle server
// @description Cloud-native media hosting & streaming server for businesses.
// @version v1
Expand Down Expand Up @@ -56,12 +61,12 @@ func RegisterRoutes(r *gin.Engine) *gin.Engine {
// @Summary Check service status
// @Description Check for the health of the service
// @Produce json
// @Success 200 {object} httputils.DataResponse{data=map[string]string}
// @Success 200 {object} httputils.DataResponse{data=HealthCheckResponse}
// @Router /health [get]
func healthCheckHandler(ctx *gin.Context) {
httputils.NewData(ctx, http.StatusOK, map[string]string{
"tag": config.Version,
"commit": config.Commit,
httputils.NewData(ctx, http.StatusOK, HealthCheckResponse{
Tag: config.Version,
Commit: config.Commit,
})
}

Expand Down
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func TestApi(t *testing.T) {
assert.NoError(err)

var body struct {
Code int `json:"code"`
Data map[string]string `json:"data,omitempty"`
Code int `json:"code"`
Data HealthCheckResponse `json:"data,omitempty"`
}
_ = json.NewDecoder(res.Body).Decode(&body)

assert.Equal(200, body.Code)
assert.Equal(config.Version, body.Data["tag"])
assert.Equal(config.Commit, body.Data["commit"])
assert.Equal(config.Version, body.Data.Tag)
assert.Equal(config.Commit, body.Data.Commit)
})
}

0 comments on commit d72be8a

Please sign in to comment.