Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add health check service and http handler (#17)
- Loading branch information
Showing
16 changed files
with
274 additions
and
5 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,45 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/allisson/postmand" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type pingResponse struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
// Ping implements interface for health check. | ||
type Ping struct { | ||
pingService postmand.PingService | ||
logger *zap.Logger | ||
} | ||
|
||
// Healthz returns health check response. | ||
func (p Ping) Healthz(w http.ResponseWriter, r *http.Request) { | ||
pr := pingResponse{} | ||
|
||
if err := p.pingService.Run(r.Context()); err != nil { | ||
p.logger.Error( | ||
"service-error", | ||
zap.String("name", "PingService"), | ||
zap.String("method", "Run"), | ||
zap.Error(err), | ||
) | ||
makeJSONResponse(w, http.StatusInternalServerError, &pr, p.logger) | ||
return | ||
} | ||
|
||
pr.Success = true | ||
makeJSONResponse(w, http.StatusOK, &pr, p.logger) | ||
} | ||
|
||
// NewPing creates a new Ping. | ||
func NewPing(pingService postmand.PingService, logger *zap.Logger) *Ping { | ||
return &Ping{ | ||
pingService: pingService, | ||
logger: logger, | ||
} | ||
} |
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,53 @@ | ||
package handler | ||
|
||
import ( | ||
"errors" | ||
nethttp "net/http" | ||
"testing" | ||
|
||
"github.com/allisson/postmand/http" | ||
"github.com/allisson/postmand/mocks" | ||
"github.com/steinfletcher/apitest" | ||
"github.com/stretchr/testify/mock" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestPing(t *testing.T) { | ||
logger, _ := zap.NewDevelopment() | ||
|
||
t.Run("With success", func(t *testing.T) { | ||
pingService := &mocks.PingService{} | ||
pingHandler := NewPing(pingService, logger) | ||
router := http.NewRouter(logger) | ||
router.Get("/healthz", pingHandler.Healthz) | ||
|
||
pingService.On("Run", mock.Anything).Return(nil) | ||
apitest.New(). | ||
Handler(router). | ||
Get("/healthz"). | ||
Expect(t). | ||
Body(`{"success":true}`). | ||
Status(nethttp.StatusOK). | ||
End() | ||
|
||
pingService.AssertExpectations(t) | ||
}) | ||
|
||
t.Run("With error", func(t *testing.T) { | ||
pingService := &mocks.PingService{} | ||
pingHandler := NewPing(pingService, logger) | ||
router := http.NewRouter(logger) | ||
router.Get("/healthz", pingHandler.Healthz) | ||
|
||
pingService.On("Run", mock.Anything).Return(errors.New("BOOM")) | ||
apitest.New(). | ||
Handler(router). | ||
Get("/healthz"). | ||
Expect(t). | ||
Body(`{"success":false}`). | ||
Status(nethttp.StatusInternalServerError). | ||
End() | ||
|
||
pingService.AssertExpectations(t) | ||
}) | ||
} |
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// Ping implements postmand.PingRepository interface. | ||
type Ping struct { | ||
db *sqlx.DB | ||
} | ||
|
||
// Run ping operation against the database. | ||
func (p Ping) Run(ctx context.Context) error { | ||
return p.db.PingContext(ctx) | ||
} | ||
|
||
// NewPing will create an implementation of postmand.PingRepository. | ||
func NewPing(db *sqlx.DB) *Ping { | ||
return &Ping{db: db} | ||
} |
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,17 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPing(t *testing.T) { | ||
ctx := context.Background() | ||
th := newTestHelper() | ||
defer th.db.Close() | ||
|
||
err := th.pingRepository.Run(ctx) | ||
assert.Nil(t, err) | ||
} |
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,22 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/allisson/postmand" | ||
) | ||
|
||
// Ping implements postmand.PingService interface. | ||
type Ping struct { | ||
pingRepository postmand.PingRepository | ||
} | ||
|
||
// Run ping operation against the database. | ||
func (p Ping) Run(ctx context.Context) error { | ||
return p.pingRepository.Run(ctx) | ||
} | ||
|
||
// NewPing will create an implementation of postmand.PingService. | ||
func NewPing(pingRepository postmand.PingRepository) *Ping { | ||
return &Ping{pingRepository: pingRepository} | ||
} |
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,20 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/allisson/postmand/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestPing(t *testing.T) { | ||
pingRepository := &mocks.PingRepository{} | ||
pingService := NewPing(pingRepository) | ||
pingRepository.On("Run", mock.Anything).Return(nil) | ||
ctx := context.Background() | ||
err := pingService.Run(ctx) | ||
assert.Nil(t, err) | ||
pingRepository.AssertExpectations(t) | ||
} |