feat: 实现配置校验与运维命令及优雅停机

This commit is contained in:
Nixevol
2026-09-30 06:47:16 +08:00
parent 22c56d1f35
commit d777a1e3d3
17 changed files with 1053 additions and 26 deletions
+38
View File
@@ -0,0 +1,38 @@
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
}