From eeb671cd0d34e48b1b5e1bbfcd66a027492014a1 Mon Sep 17 00:00:00 2001 From: black1552 Date: Fri, 6 Mar 2026 10:45:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(gateway):=20=E4=BF=AE=E5=A4=8D=E5=8F=8D?= =?UTF-8?q?=E5=90=91=E4=BB=A3=E7=90=86=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=92=8C=E8=AF=B7=E6=B1=82=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 WebSocket 协议支持说明 - 移除不必要的 c.Abort() 调用以避免中断请求处理 - 使用标准 http.Error 替代 gin.JSON 进行错误响应 - 修复代理错误处理中的响应写入问题 --- .idea/GOHCache.xml | 2 +- server/gateway.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.idea/GOHCache.xml b/.idea/GOHCache.xml index 74951f5..3480849 100644 --- a/.idea/GOHCache.xml +++ b/.idea/GOHCache.xml @@ -346,7 +346,7 @@ - diff --git a/server/gateway.go b/server/gateway.go index 1cfcd0a..fea5c9c 100644 --- a/server/gateway.go +++ b/server/gateway.go @@ -10,6 +10,7 @@ import ( ) // BuildRequest 创建 HTTP 反向代理 Handler +// 支持 HTTP 和 WebSocket 协议 // @Param host 目标服务器地址,例如 "http://127.0.0.1:8081" func BuildRequest(host string) gin.HandlerFunc { return func(c *gin.Context) { @@ -17,7 +18,6 @@ func BuildRequest(host string) gin.HandlerFunc { if err != nil { log.Error("parse target host error:", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid target host"}) - c.Abort() return } @@ -37,10 +37,9 @@ func BuildRequest(host string) gin.HandlerFunc { // 处理代理错误 proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) { log.Error("proxy error:", err) - c.JSON(http.StatusBadGateway, gin.H{"error": "proxy error: " + err.Error()}) + http.Error(w, "proxy error: "+err.Error(), http.StatusBadGateway) } proxy.ServeHTTP(c.Writer, c.Request) - c.Abort() } }