package app import ( "fmt" "io" "net" "net/http" "strings" "time" ) // Healthcheck asks the local daemon for /api/health. It is what the container // HEALTHCHECK runs (`urutau healthcheck`), so the image needs no curl. func Healthcheck(c Config) error { host, port, err := net.SplitHostPort(c.Bind) if err != nil { return err } if host == "" || host == "0.0.0.0" || host == "::" { host = "127.0.0.1" } cl := &http.Client{Timeout: 3 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} resp, err := cl.Get("http://" + net.JoinHostPort(host, port) + "/api/health") if err != nil { return err } defer resp.Body.Close() b, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) if resp.StatusCode != 200 || !strings.Contains(string(b), `"status":"ok"`) { return fmt.Errorf("unhealthy: HTTP %d", resp.StatusCode) } return nil }