-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
423 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package domain | ||
|
||
import "context" | ||
|
||
// HealthCheck entity. | ||
type HealthCheck struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
// HealthCheckRepository is the repository interface for the HealthCheck entity. | ||
type HealthCheckRepository interface { | ||
Check(ctx context.Context) (*HealthCheck, error) | ||
} | ||
|
||
// HealthCheckService is the service interface for the HealthCheck entity. | ||
type HealthCheckService interface { | ||
Check(ctx context.Context) (*HealthCheck, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/allisson/psqlqueue/domain" | ||
) | ||
|
||
// nolint:unused | ||
type healthCheckResponse struct { | ||
Success bool `json:"success"` | ||
} //@name HealthCheckResponse | ||
|
||
// HealthCheckHandler exposes a REST API for domain.HealthCheckService. | ||
type HealthCheckHandler struct { | ||
healthCheckService domain.HealthCheckService | ||
} | ||
|
||
// Execute a health check. | ||
// | ||
// @Summary Execute a health check | ||
// @Tags health-check | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {object} healthCheckResponse | ||
// @Failure 500 {object} errorResponse | ||
// @Router /healthz [get] | ||
func (h *HealthCheckHandler) Check(c *gin.Context) { | ||
healthCheck, err := h.healthCheckService.Check(c.Request.Context()) | ||
if err != nil { | ||
er := parseServiceError("healthCheckService", "Check", err) | ||
c.JSON(er.StatusCode, &er) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, &healthCheck) | ||
} | ||
|
||
// NewHealthCheckHandler returns a new HealthCheckHandler. | ||
func NewHealthCheckHandler(healthCheckService domain.HealthCheckService) *HealthCheckHandler { | ||
return &HealthCheckHandler{healthCheckService: healthCheckService} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/allisson/psqlqueue/domain" | ||
) | ||
|
||
func TestHealthCheckHandler(t *testing.T) { | ||
t.Run("Check", func(t *testing.T) { | ||
expectedPayload := `{"success":true}` | ||
tc := makeTestContext(t) | ||
reqRec := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/v1/healthz", nil) | ||
|
||
tc.healthCheckService.On("Check", mock.Anything).Return(&domain.HealthCheck{Success: true}, nil) | ||
tc.router.ServeHTTP(reqRec, req) | ||
|
||
assert.Equal(t, http.StatusOK, reqRec.Code) | ||
assert.Equal(t, expectedPayload, reqRec.Body.String()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.