fix(agent): health check logic

This commit is contained in:
yusing 2025-05-11 00:05:01 +08:00
parent 30c76cfc5f
commit 84e7a6591e

View file

@ -18,7 +18,7 @@ func CheckHealth(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
scheme := query.Get("scheme") scheme := query.Get("scheme")
if scheme == "" { if scheme == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, "missing scheme", http.StatusBadRequest)
return return
} }
@ -28,7 +28,7 @@ func CheckHealth(w http.ResponseWriter, r *http.Request) {
case "fileserver": case "fileserver":
path := query.Get("path") path := query.Get("path")
if path == "" { if path == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, "missing path", http.StatusBadRequest)
return return
} }
_, err := os.Stat(path) _, err := os.Stat(path)
@ -40,7 +40,7 @@ func CheckHealth(w http.ResponseWriter, r *http.Request) {
host := query.Get("host") host := query.Get("host")
path := query.Get("path") path := query.Get("path")
if host == "" { if host == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, "missing host", http.StatusBadRequest)
return return
} }
result, err = monitor.NewHTTPHealthMonitor(&url.URL{ result, err = monitor.NewHTTPHealthMonitor(&url.URL{
@ -51,17 +51,18 @@ func CheckHealth(w http.ResponseWriter, r *http.Request) {
case "tcp", "udp": case "tcp", "udp":
host := query.Get("host") host := query.Get("host")
if host == "" { if host == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, "missing host", http.StatusBadRequest)
return return
} }
hasPort := strings.Contains(host, ":") hasPort := strings.Contains(host, ":")
port := query.Get("port") port := query.Get("port")
if port != "" && !hasPort { if port != "" && hasPort {
host = fmt.Sprintf("%s:%s", host, port) http.Error(w, "port and host with port cannot both be provided", http.StatusBadRequest)
} else {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
if port != "" {
host = fmt.Sprintf("%s:%s", host, port)
}
result, err = monitor.NewRawHealthMonitor(&url.URL{ result, err = monitor.NewRawHealthMonitor(&url.URL{
Scheme: scheme, Scheme: scheme,
Host: host, Host: host,