super-graph/serv/health.go

33 lines
781 B
Go
Raw Permalink Normal View History

package serv
import (
"context"
"net/http"
)
var healthyResponse = []byte("All's Well")
func health(w http.ResponseWriter, _ *http.Request) {
conn, err := db.Acquire(context.Background())
if err != nil {
errlog.Error().Err(err).Msg("error acquiring connection from pool")
w.WriteHeader(http.StatusInternalServerError)
return
}
2019-12-25 07:24:30 +01:00
ctx, cancel := context.WithTimeout(context.Background(), conf.DB.PingTimeout)
defer cancel()
if err := conn.Conn().Ping(ctx); err != nil {
errlog.Error().Err(err).Msg("error pinging database")
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(healthyResponse); err != nil {
errlog.Error().Err(err).Msg("error writing healthy response")
w.WriteHeader(http.StatusInternalServerError)
return
}
}