Skip to content

Commit

Permalink
Add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Schettini committed Apr 17, 2021
1 parent 962a9b2 commit 9ba0578
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
4 changes: 4 additions & 0 deletions gaivota.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ type OrderStore interface {
// Update the Order in the store.
Update(context.Context, *Order) error
}

type HealthChecker interface {
Ping() (msg string, err error)
}
21 changes: 0 additions & 21 deletions handlers/healthcheck.go

This file was deleted.

34 changes: 34 additions & 0 deletions pkg/mux/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mux

import (
"log"
"net/http"

"github.com/leoschet/gaivota"
)

type HealthCheck struct {
logger *log.Logger
dependencies []gaivota.HealthChecker
}

func NewHealthCheck(logger *log.Logger, dependencies []gaivota.HealthChecker) *HealthCheck {
return &HealthCheck{
logger,
dependencies,
}
}

func (hc *HealthCheck) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
for _, dep := range hc.dependencies {
msg, err := dep.Ping()
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(msg))
return
}
}

rw.WriteHeader(http.StatusOK)
rw.Write([]byte("pong"))
}
10 changes: 10 additions & 0 deletions pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func (db *Database) Close() {
db.Pool.Close()
}

func (db *Database) Ping() (msg string, err error) {
err = db.Pool.Ping(context.Background())

if err != nil {
return "Could not connect to the Database", err
}

return "", nil
}

// func (db *Database) CreateTable(model interface{}) {
// // Get how many properties are in the model
// column_count := reflect.ValueOf(model).NumField()
Expand Down

0 comments on commit 9ba0578

Please sign in to comment.