Skip to content

Commit

Permalink
Adding alive endpoint (#17)
Browse files Browse the repository at this point in the history
* feat: add alive endpoint and send config pointer to healthcheck handlers

* fix: improve alive type
  • Loading branch information
GuilhermeMLS committed Feb 9, 2022
1 parent 30fbdef commit 362febe
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/go_service_template/main.go
Expand Up @@ -20,7 +20,7 @@ func main() {
config.IsDevelopmentEnvironment(),
)
app := fiber.New()
healthcheckrouter.RegisterRoutes(app, logger)
healthcheckrouter.RegisterRoutes(app, logger, config)
logger.Info("\"🧜‍ Core APIs Go Service Template Listening on port: " + config.Port)
log.Fatal(app.Listen(":" + config.Port))
}
18 changes: 15 additions & 3 deletions internal/health-check/handlers/health_check_handler.go
@@ -1,10 +1,22 @@
package healthcheck

import "go-service-template/pkg/logger"
import (
"go-service-template/internal/configuration"
"go-service-template/pkg/logger"
)

func GetStatus(logger *logger.Logger) map[string]interface{} {
// GetStatus add your health check logic here
func GetStatus(logger *logger.Logger, config *configuration.AppConfig) map[string]interface{} {
logger.Info("Executing Health Check...")
return map[string]interface{}{
"status": "OK",
"serviceName": config.ServiceName,
"status": "OK",
"environment": config.ApplicationEnv,
}
}

func Alive() map[string]bool {
return map[string]bool{
"alive": true,
}
}
8 changes: 6 additions & 2 deletions internal/health-check/handlers/health_check_handler_test.go
@@ -1,6 +1,7 @@
package healthcheck_test

import (
"go-service-template/internal/configuration"
"go-service-template/pkg/logger"
"go-service-template/pkg/utils"
"testing"
Expand All @@ -16,11 +17,14 @@ func TestHealthCheck(t *testing.T) {
utils.ClockMock{},
true,
)
config, _ := configuration.Load()
t.Run("GetStatus", func(t *testing.T) {
t.Run("It should match status message", func(t *testing.T) {
status := healthCheckHandler.GetStatus(Logger)
status := healthCheckHandler.GetStatus(Logger, config)
expected := map[string]interface{}{
"status": "OK",
"serviceName": config.ServiceName,
"status": "OK",
"environment": config.ApplicationEnv,
}
assert.Equal(t, status, expected)
})
Expand Down
9 changes: 7 additions & 2 deletions internal/health-check/routes/health_check_router.go
@@ -1,14 +1,19 @@
package healthcheckrouter

import (
"go-service-template/internal/configuration"
healthCheckHandler "go-service-template/internal/health-check/handlers"
"go-service-template/pkg/logger"

"github.com/gofiber/fiber/v2"
)

func RegisterRoutes(app *fiber.App, logger *logger.Logger) {
func RegisterRoutes(app *fiber.App, logger *logger.Logger, config *configuration.AppConfig) {
app.Get("/health-check", func(c *fiber.Ctx) error {
return c.Status(200).JSON(healthCheckHandler.GetStatus(logger))
return c.Status(200).JSON(healthCheckHandler.GetStatus(logger, config))
})

app.Get("/alive", func(c *fiber.Ctx) error {
return c.Status(200).JSON(healthCheckHandler.Alive())
})
}
5 changes: 3 additions & 2 deletions internal/health-check/routes/health_check_router_test.go
Expand Up @@ -2,6 +2,7 @@ package healthcheckrouter_test

import (
"fmt"
"go-service-template/internal/configuration"
healthCheckRouter "go-service-template/internal/health-check/routes"
"go-service-template/pkg/logger"
"go-service-template/pkg/utils"
Expand All @@ -19,9 +20,9 @@ func TestHealthCheckRouter(t *testing.T) {
utils.ClockMock{},
true,
)

config, _ := configuration.Load()
app := fiber.New()
healthCheckRouter.RegisterRoutes(app, Logger)
healthCheckRouter.RegisterRoutes(app, Logger, config)

t.Run("Health check handler", func(t *testing.T) {
t.Run("It should return status 200", func(t *testing.T) {
Expand Down

0 comments on commit 362febe

Please sign in to comment.