mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 17:56:37 +08:00
fix(system): expose only GET /api/healthz
Remove /healthz and /api/health so the process advertises a single probe at /api/healthz with {status: ok}.
This commit is contained in:
@@ -134,7 +134,7 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
|||||||
"/api/v1/user/send-email-code",
|
"/api/v1/user/send-email-code",
|
||||||
"/api/v1/cap/challenge",
|
"/api/v1/cap/challenge",
|
||||||
"/api/v1/cap/redeem",
|
"/api/v1/cap/redeem",
|
||||||
"/healthz",
|
"/api/healthz",
|
||||||
"/metrics",
|
"/metrics",
|
||||||
}
|
}
|
||||||
RegisterWhitelist(publicEndpoints...)
|
RegisterWhitelist(publicEndpoints...)
|
||||||
|
|||||||
@@ -15,56 +15,40 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHealthRouteReturnsOKNil(t *testing.T) {
|
func TestHealthzIsTheOnlyHealthRoute(t *testing.T) {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
ctx := core.NewContext(context.Background())
|
ctx := core.NewContext(context.Background())
|
||||||
if err := New().Apply(ctx); err != nil {
|
if err := New().Apply(ctx); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
healthHandler := routeHandler(t, ctx, "GET", "/api/health")
|
var hasHealthz bool
|
||||||
routeHandler(t, ctx, "GET", "/healthz")
|
for _, rd := range ctx.Router().Routes() {
|
||||||
if !ctx.Router().IsWhitelisted("/api/health") {
|
key := rd.Method + " " + rd.Path
|
||||||
t.Fatal("GET /api/health is not whitelisted")
|
switch key {
|
||||||
|
case "GET /api/healthz":
|
||||||
|
hasHealthz = true
|
||||||
|
case "GET /healthz", "GET /api/health":
|
||||||
|
t.Errorf("removed health route still registered: %s", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasHealthz {
|
||||||
|
t.Fatal("GET /api/healthz missing")
|
||||||
|
}
|
||||||
|
if !ctx.Router().IsWhitelisted("/api/healthz") {
|
||||||
|
t.Fatal("GET /api/healthz not whitelisted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handler := routeHandler(t, ctx, "GET", "/api/healthz")
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := gin.CreateTestContext(w)
|
c, _ := gin.CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/healthz", nil)
|
||||||
healthHandler(c)
|
handler(c)
|
||||||
|
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
|
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
var body struct {
|
|
||||||
ErrorMsg string `json:"error_msg"`
|
|
||||||
Data any `json:"data"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if body.ErrorMsg != "" {
|
|
||||||
t.Fatalf("error_msg = %q, want empty", body.ErrorMsg)
|
|
||||||
}
|
|
||||||
if body.Data != nil {
|
|
||||||
t.Fatalf("data = %#v, want null", body.Data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHealthzRouteUnchanged(t *testing.T) {
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
ctx := core.NewContext(context.Background())
|
|
||||||
if err := New().Apply(ctx); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
handler := routeHandler(t, ctx, "GET", "/healthz")
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
c, _ := gin.CreateTestContext(w)
|
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
|
||||||
handler(c)
|
|
||||||
|
|
||||||
var body map[string]any
|
var body map[string]any
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
@@ -50,14 +50,10 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
|||||||
appName := ctx.Config().String("app.app_name", "Wavelet")
|
appName := ctx.Config().String("app.app_name", "Wavelet")
|
||||||
|
|
||||||
// 1. Health check
|
// 1. Health check
|
||||||
ctx.Router().GET("/healthz", func(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
||||||
})
|
|
||||||
ctx.Router().GET("/api/healthz", func(c *gin.Context) {
|
ctx.Router().GET("/api/healthz", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
})
|
})
|
||||||
ctx.Router().GET("/api/health", Health)
|
ctx.Router().RegisterWhitelist("/api/healthz")
|
||||||
ctx.Router().RegisterWhitelist("/api/health")
|
|
||||||
|
|
||||||
// 2. Public config
|
// 2. Public config
|
||||||
ctx.Router().GET("/api/v1/config/public", func(c *gin.Context) {
|
ctx.Router().GET("/api/v1/config/public", func(c *gin.Context) {
|
||||||
@@ -90,14 +86,3 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Health 健康检查
|
|
||||||
// @Summary 健康检查
|
|
||||||
// @Description 检查服务是否正常运行,可用于负载均衡存活探测
|
|
||||||
// @Tags health
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200 {object} response.Any{data=string} "服务正常"
|
|
||||||
// @Router /api/health [get]
|
|
||||||
func Health(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, response.OKNil())
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user