Files
NixMsg/cmd/nixmsg/healthcheck.go

39 lines
856 B
Go

package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
func cmdHealthcheck(_ []string) error {
cfg, err := loadAndValidateConfig()
if err != nil {
return err
}
addrBytes, err := os.ReadFile(filepath.Join(cfg.DataDir, "listen.addr"))
if err != nil {
return fmt.Errorf("read listen.addr: %w", err)
}
addr := strings.TrimSpace(string(addrBytes))
if addr == "" {
return fmt.Errorf("listen.addr is empty")
}
url := "http://" + addr + "/healthz"
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("healthz status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
return nil
}