GoBlog/healthcheck.go

39 lines
783 B
Go
Raw Normal View History

2021-04-11 14:08:29 +00:00
package main
import (
2021-06-19 21:54:07 +00:00
"context"
2021-04-11 14:08:29 +00:00
"fmt"
2021-10-13 07:01:54 +00:00
"log"
2021-04-11 14:08:29 +00:00
"net/http"
2021-06-19 21:54:07 +00:00
"time"
2021-04-11 14:08:29 +00:00
)
func (a *goBlog) healthcheck() bool {
2021-10-13 07:01:54 +00:00
if a.tailscaleEnabled() {
log.Println("Skip healthcheck because Tailscale is enabled")
return true
}
2021-06-19 21:54:07 +00:00
timeoutContext, cancelFunc := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFunc()
req, err := http.NewRequestWithContext(timeoutContext, http.MethodGet, a.getFullAddress("/ping"), nil)
2021-04-11 14:08:29 +00:00
if err != nil {
2022-02-12 11:37:13 +00:00
fmt.Println("healthcheck:", err.Error())
2021-04-11 14:08:29 +00:00
return false
}
2021-06-19 06:37:16 +00:00
resp, err := a.httpClient.Do(req)
2021-04-11 14:08:29 +00:00
if err != nil {
2022-02-12 11:37:13 +00:00
fmt.Println("healthcheck:", err.Error())
2021-04-11 14:08:29 +00:00
return false
}
defer resp.Body.Close()
return resp.StatusCode == 200
}
2021-05-07 14:14:15 +00:00
func (a *goBlog) healthcheckExitCode() int {
if a.healthcheck() {
2021-05-07 14:14:15 +00:00
return 0
} else {
return 1
}
}