feat: add health check endpoint and update Makefile for health URL

This commit is contained in:
mlogclub
2026-06-13 09:59:21 +08:00
parent b8a7dd022c
commit 164953d56d
4 changed files with 58 additions and 1 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ GO ?= go
PNPM ?= pnpm
GOOS ?= $(shell $(GO) env GOOS)
GOARCH ?= $(shell $(GO) env GOARCH)
DEV_SERVER_URL ?= http://127.0.0.1:8083
DEV_SERVER_URL ?= http://127.0.0.1:8083/api/health
LANCEDB_VERSION ?= v0.1.2
LANCEDB_DOWNLOAD_SCRIPT ?= https://raw.githubusercontent.com/lancedb/lancedb-go/main/scripts/download-artifacts.sh
LANCEDB ?= 0
+2
View File
@@ -8,6 +8,7 @@ import (
"agent-desk/internal/ai/mcps"
_ "agent-desk/internal/ai/runtime"
"agent-desk/internal/handlers/api"
"agent-desk/internal/middleware"
"agent-desk/internal/pkg/config"
"agent-desk/internal/pkg/ginx"
@@ -154,6 +155,7 @@ func addRouter(app *gin.Engine) {
app.Any("/api/mcp", gin.WrapH(mcps.NewHTTPHandler()))
apiGroup := app.Group("/api")
apiGroup.GET("/health", api.Health)
registerApiAuthRoutes(apiGroup.Group("/auth"))
registerApiChannelRoutes(apiGroup.Group("/channel"))
registerApiCustomerRoutes(apiGroup.Group("/customer"))
+40
View File
@@ -32,6 +32,7 @@ func TestNewServerRegistersGinRoutes(t *testing.T) {
expected := []string{
http.MethodPost + " /api/auth/login",
http.MethodGet + " /api/health",
http.MethodGet + " /api/auth/oidc_login",
http.MethodGet + " /api/auth/oidc_callback",
http.MethodPost + " /api/auth/oidc_exchange",
@@ -50,6 +51,45 @@ func TestNewServerRegistersGinRoutes(t *testing.T) {
}
}
func TestNewServerHealthEndpointIsPublic(t *testing.T) {
config.SetCurrent(&config.Config{
Storage: config.StorageConfig{
Local: config.LocalStorageConfig{
Root: "storage",
BaseURL: "/storage",
},
},
})
app, err := NewServer()
if err != nil {
t.Fatalf("NewServer() error = %v", err)
}
rec := httptest.NewRecorder()
app.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/health", nil))
if rec.Code != http.StatusOK {
t.Fatalf("status=%d want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
var body struct {
Success bool `json:"success"`
Data struct {
Status string `json:"status"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if !body.Success {
t.Fatalf("success=false, body=%s", rec.Body.String())
}
if body.Data.Status != "ok" {
t.Fatalf("status=%q want ok", body.Data.Status)
}
}
func TestNewServerExposesPublicAuthOptions(t *testing.T) {
config.SetCurrent(&config.Config{
Storage: config.StorageConfig{
+15
View File
@@ -0,0 +1,15 @@
package api
import (
"agent-desk/internal/pkg/httpx"
"github.com/gin-gonic/gin"
)
type healthResponse struct {
Status string `json:"status"`
}
func Health(ctx *gin.Context) {
httpx.WriteJSON(ctx, &healthResponse{Status: "ok"})
}