123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package health
- import (
- "fmt"
- "net/http"
- "time"
- "github.com/go-chi/chi"
- "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/dao"
- "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/logging"
- )
- // global storage definition
- var myhealthy bool
- var log logging.ServiceLogger
- /*
- This is the healtchcheck you will have to provide.
- */
- func check() (bool, string) {
- myhealthy = true
- message := ""
- err := dao.GetStorage().Ping()
- if err != nil {
- myhealthy = false
- message = err.Error()
- }
- return myhealthy, message
- }
- //##### template internal functions for processing the healthchecks #####
- var healthmessage string
- var healthy bool
- var lastChecked time.Time
- var period int
- // CheckConfig configuration for the healthcheck system
- type CheckConfig struct {
- Period int
- }
- // InitHealthSystem initialise the complete health system
- func InitHealthSystem(config CheckConfig) {
- period = config.Period
- log.Infof("healthcheck starting with period: %d seconds", period)
- healthmessage = "service starting"
- healthy = false
- doCheck()
- go func() {
- background := time.NewTicker(time.Second * time.Duration(period))
- for range background.C {
- doCheck()
- }
- }()
- }
- /*
- internal function to process the health check
- */
- func doCheck() {
- var msg string
- healthy, msg = check()
- if !healthy {
- healthmessage = msg
- } else {
- healthmessage = ""
- }
- lastChecked = time.Now()
- }
- /*
- Routes getting all routes for the health endpoint
- */
- func Routes() *chi.Mux {
- router := chi.NewRouter()
- router.Get("/health", GetHealthyEndpoint)
- router.Get("/readiness", GetReadinessEndpoint)
- return router
- }
- /*
- GetHealthyEndpoint is this service healthy
- */
- func GetHealthyEndpoint(response http.ResponseWriter, req *http.Request) {
- t := time.Now()
- if t.Sub(lastChecked) > (time.Second * time.Duration(2*period)) {
- healthy = false
- healthmessage = "Healthcheck not running"
- }
- response.Header().Add("Content-Type", "application/json")
- if healthy {
- response.WriteHeader(http.StatusOK)
- message := fmt.Sprintf(`{ "message": "service up and running", "lastCheck": "%s" }`, lastChecked.String())
- response.Write([]byte(message))
- } else {
- response.WriteHeader(http.StatusServiceUnavailable)
- message := fmt.Sprintf(`{ "message": "service is unavailable: %s", "lastCheck": "%s" }`, healthmessage, lastChecked.String())
- response.Write([]byte(message))
- }
- }
- /*
- GetReadinessEndpoint is this service ready for taking requests
- */
- func GetReadinessEndpoint(response http.ResponseWriter, req *http.Request) {
- response.Header().Add("Content-Type", "application/json")
- response.WriteHeader(http.StatusOK)
- response.Write([]byte(`{ "message": "service started" }`))
- }
|