1
mirror of https://github.com/jlelse/GoBlog synced 2024-06-15 11:37:09 +00:00
GoBlog/healthcheck.go

34 lines
667 B
Go

package main
import (
"context"
"fmt"
"net/http"
"time"
)
func (a *goBlog) healthcheck() bool {
timeoutContext, cancelFunc := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFunc()
req, err := http.NewRequestWithContext(timeoutContext, http.MethodGet, a.getFullAddress("/ping"), nil)
if err != nil {
fmt.Println("healthcheck:", err.Error())
return false
}
resp, err := a.httpClient.Do(req)
if err != nil {
fmt.Println("healthcheck:", err.Error())
return false
}
_ = resp.Body.Close()
return resp.StatusCode == 200
}
func (a *goBlog) healthcheckExitCode() int {
if a.healthcheck() {
return 0
} else {
return 1
}
}