fix(gateway): 修复反向代理错误处理和请求流程
- 添加 WebSocket 协议支持说明 - 移除不必要的 c.Abort() 调用以避免中断请求处理 - 使用标准 http.Error 替代 gin.JSON 进行错误响应 - 修复代理错误处理中的响应写入问题main
parent
3096d7dedd
commit
eeb671cd0d
|
|
@ -346,7 +346,7 @@
|
||||||
<entry key="file://$PROJECT_DIR$/server/gateway.go">
|
<entry key="file://$PROJECT_DIR$/server/gateway.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1772764771892" />
|
<option name="lastModified" value="1772765089927" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// BuildRequest 创建 HTTP 反向代理 Handler
|
// BuildRequest 创建 HTTP 反向代理 Handler
|
||||||
|
// 支持 HTTP 和 WebSocket 协议
|
||||||
// @Param host 目标服务器地址,例如 "http://127.0.0.1:8081"
|
// @Param host 目标服务器地址,例如 "http://127.0.0.1:8081"
|
||||||
func BuildRequest(host string) gin.HandlerFunc {
|
func BuildRequest(host string) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
|
@ -17,7 +18,6 @@ func BuildRequest(host string) gin.HandlerFunc {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("parse target host error:", err)
|
log.Error("parse target host error:", err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid target host"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid target host"})
|
||||||
c.Abort()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,10 +37,9 @@ func BuildRequest(host string) gin.HandlerFunc {
|
||||||
// 处理代理错误
|
// 处理代理错误
|
||||||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
log.Error("proxy error:", err)
|
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)
|
proxy.ServeHTTP(c.Writer, c.Request)
|
||||||
c.Abort()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue