Compare commits

..
1 Commits
Author SHA1 Message Date
adminandClaude Code c430a4ed86 feat(api): 支持 -healthcheck 参数检查 HTTP 健康状态
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-12 17:15:22 +08:00
+28
View File
@@ -3,6 +3,7 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net/http"
@@ -10,6 +11,7 @@ import (
"os/signal"
"strconv"
"syscall"
"time"
"git.vitatech.com.cn/leo/proto.git/micro_layout/client"
@@ -19,12 +21,38 @@ import (
)
func main() {
healthcheckOnly := flag.Bool("healthcheck", false, "按服务配置检查健康状态并退出")
flag.Parse()
if *healthcheckOnly {
if err := checkHealth(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
if err := run(); err != nil {
slog.Error("application stopped", "error", err)
os.Exit(1)
}
}
func checkHealth() error {
cfg, err := config.Load()
if err != nil {
return err
}
client := &http.Client{Timeout: 3 * time.Second}
response, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/health", cfg.Server.Port))
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP healthcheck returned status %d", response.StatusCode)
}
return nil
}
func run() error {
cfg, err := config.Load()
if err != nil {