mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 16:58:31 +02:00
fix(metrics): non ws response being encoded twice; simplified response handling
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
"maps"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -52,25 +54,22 @@ func SystemInfo(c *gin.Context) {
|
|||||||
|
|
||||||
isWS := httpheaders.IsWebsocket(c.Request.Header)
|
isWS := httpheaders.IsWebsocket(c.Request.Header)
|
||||||
if !isWS {
|
if !isWS {
|
||||||
respData, status, err := agent.Forward(c.Request, agentPkg.EndpointSystemInfo)
|
resp, err := agent.Forward(c.Request, agentPkg.EndpointSystemInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Error(apitypes.InternalServerError(err, "failed to forward request to agent"))
|
c.Error(apitypes.InternalServerError(err, "failed to forward request to agent"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if status != http.StatusOK {
|
maps.Copy(c.Writer.Header(), resp.Header)
|
||||||
c.JSON(status, apitypes.Error(string(respData)))
|
c.Status(resp.StatusCode)
|
||||||
return
|
io.Copy(c.Writer, resp.Body)
|
||||||
}
|
|
||||||
c.JSON(status, respData)
|
|
||||||
} else {
|
} else {
|
||||||
rp := reverseproxy.NewReverseProxy("agent", nettypes.NewURL(agentPkg.AgentURL), agent.Transport())
|
rp := reverseproxy.NewReverseProxy("agent", nettypes.NewURL(agentPkg.AgentURL), agent.Transport())
|
||||||
header := c.Request.Header.Clone()
|
r, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, agentPkg.EndpointSystemInfo+"?"+query.Encode(), c.Request.Body)
|
||||||
r, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, agentPkg.EndpointSystemInfo+"?"+query.Encode(), nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Error(apitypes.InternalServerError(err, "failed to create request"))
|
c.Error(apitypes.InternalServerError(err, "failed to create request"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.Header = header
|
r.Header = c.Request.Header
|
||||||
rp.ServeHTTP(c.Writer, r)
|
rp.ServeHTTP(c.Writer, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user