Files
NixMsg/test/accept/routes.go
T

66 lines
1.6 KiB
Go

package accept
import (
"bytes"
"io"
"net/http"
"strings"
"time"
)
// RouteStatus 探测结果。
type RouteStatus int
const (
RouteMissing RouteStatus = iota // 404 / 无此路由
RoutePresent // 路由存在(含 4xx/5xx 业务响应)
)
// ProbeMethod 对运行中的服务发一次 HTTP 请求。
func ProbeMethod(base, method, path string, body []byte) (statusCode int, bodyText string, err error) {
var rdr io.Reader
if body != nil {
rdr = bytes.NewReader(body)
}
req, err := http.NewRequest(method, strings.TrimRight(base, "/")+path, rdr)
if err != nil {
return 0, "", err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return 0, "", err
}
defer func() { _ = resp.Body.Close() }()
b, _ := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
return resp.StatusCode, string(b), nil
}
// ClassifyAdminLogin 判断管理登录是否已挂到进程。
// 404 → 未挂载;其它(含 400/401/429)视为已挂载。
func ClassifyAdminLogin(code int) RouteStatus {
if code == http.StatusNotFound {
return RouteMissing
}
return RoutePresent
}
// ClassifyRegister 判断注册路由是否已挂到进程。
func ClassifyRegister(code int) RouteStatus {
if code == http.StatusNotFound {
return RouteMissing
}
return RoutePresent
}
// ClassifyCreateEndpoint 判断开通端路由是否已挂到进程。
func ClassifyCreateEndpoint(code int) RouteStatus {
if code == http.StatusNotFound {
return RouteMissing
}
return RoutePresent
}