52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.vakhrushev.me/av/jellybit/internal/config"
|
|
)
|
|
|
|
// runHealthcheck дёргает /healthz локального сервиса и завершается с кодом 0
|
|
// при 200, иначе ненулевым. Нужен для HEALTHCHECK в distroless-образе, где
|
|
// нет shell/curl: docker зовёт сам бинарь.
|
|
func runHealthcheck(args []string) error {
|
|
fs := flag.NewFlagSet("healthcheck", flag.ContinueOnError)
|
|
configPath := fs.String("config", "/config/config.toml", "путь к config.toml")
|
|
if err := fs.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// listen вида ":8080" или "127.0.0.1:8080" → стучимся на localhost:<port>.
|
|
_, port, err := net.SplitHostPort(cfg.HTTP.Listen)
|
|
if err != nil {
|
|
return fmt.Errorf("parse http.listen %q: %w", cfg.HTTP.Listen, err)
|
|
}
|
|
url := "http://127.0.0.1:" + port + "/healthz"
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("healthcheck request: %w", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("healthcheck: status %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|