fix: 修复若干问题,添加java lsp
This commit is contained in:
@@ -22,10 +22,17 @@ type SessionStatsProvider interface {
|
||||
ActiveSessions() map[string]int
|
||||
}
|
||||
|
||||
// LSPStatusProvider 暴露按语言的 LSP 探测状态。
|
||||
type LSPStatusProvider interface {
|
||||
LspServiceStatus() map[string]any
|
||||
}
|
||||
|
||||
// RouteOptions 控制 HTTP/WS 接口的超时与请求体上限。
|
||||
type RouteOptions struct {
|
||||
RequestTimeout time.Duration // 单次补全调用超时时间。
|
||||
MaxBodyBytes int64 // 请求体最大字节数(HTTP/WS 共用)。
|
||||
// LSPStatusProvider 可选,用于输出语言服务在线状态。
|
||||
LSPStatusProvider LSPStatusProvider
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册健康检查、HTTP 补全接口和 WebSocket 补全接口。
|
||||
@@ -41,6 +48,7 @@ func RegisterRoutes(router *gin.Engine, service CompletionService, options ...Ro
|
||||
if options[0].MaxBodyBytes > 0 {
|
||||
opts.MaxBodyBytes = options[0].MaxBodyBytes
|
||||
}
|
||||
opts.LSPStatusProvider = options[0].LSPStatusProvider
|
||||
}
|
||||
|
||||
router.GET("/health", func(c *gin.Context) {
|
||||
@@ -62,6 +70,17 @@ func RegisterRoutes(router *gin.Engine, service CompletionService, options ...Ro
|
||||
})
|
||||
})
|
||||
|
||||
router.GET("/health/lsp-status", func(c *gin.Context) {
|
||||
languages := map[string]any{}
|
||||
if opts.LSPStatusProvider != nil {
|
||||
languages = opts.LSPStatusProvider.LspServiceStatus()
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "ok",
|
||||
"languages": languages,
|
||||
})
|
||||
})
|
||||
|
||||
registerWSRoutes(router, service, opts)
|
||||
|
||||
handleCompletion := func(c *gin.Context) {
|
||||
|
||||
@@ -28,6 +28,14 @@ func (f *fakeCompletionService) Complete(_ context.Context, _ completion.Request
|
||||
return f.resp, nil
|
||||
}
|
||||
|
||||
type fakeLSPStatusProvider struct {
|
||||
status map[string]any
|
||||
}
|
||||
|
||||
func (f *fakeLSPStatusProvider) LspServiceStatus() map[string]any {
|
||||
return f.status
|
||||
}
|
||||
|
||||
// 验证 HTTP 补全接口的成功路径。
|
||||
func TestRegisterRoutesCompletionSuccess(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
@@ -174,3 +182,41 @@ func TestRegisterRoutesCompletionWebSocketSuccess(t *testing.T) {
|
||||
t.Fatalf("unexpected items: %+v", resp.Items)
|
||||
}
|
||||
}
|
||||
|
||||
// 验证 /health/lsp-status 会返回语言探测状态快照。
|
||||
func TestRegisterRoutesLspStatus(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
RegisterRoutes(r, &fakeCompletionService{}, RouteOptions{
|
||||
LSPStatusProvider: &fakeLSPStatusProvider{
|
||||
status: map[string]any{
|
||||
"go": map[string]any{
|
||||
"online": true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/health/lsp-status", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
if got["status"] != "ok" {
|
||||
t.Fatalf("expected status ok, got %#v", got["status"])
|
||||
}
|
||||
languages, ok := got["languages"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("languages field type mismatch: %#v", got["languages"])
|
||||
}
|
||||
if _, ok := languages["go"]; !ok {
|
||||
t.Fatalf("expected go language status, got %#v", languages)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user