From e29e8d8049b450125289dac7a378512b47cb77f0 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Fri, 22 Sep 2023 11:55:39 +0800 Subject: [PATCH] proxyserver --- proxyserver/main.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/proxyserver/main.go b/proxyserver/main.go index c98a5d15..91ffa076 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -104,18 +104,8 @@ func main() { // 对/api开头的请求进行反向代理 proxy := httputil.NewSingleHostReverseProxy(apiURL) - // proxy.ErrorHandler = func(res http.ResponseWriter, req *http.Request, err error) { - // if err != nil { - - // // 在发生错误时进行处理 - // log.Println(res.Header()) - // logx.Info(err) - // logx.Info(res.Header()) - // res.WriteHeader(http.StatusNotFound) // 返回404状态码 - // } - // } proxy.ServeHTTP(w, r) - log.Println(w.Header()) + return } else { @@ -258,6 +248,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac } conn, err := upgrader.Upgrade(w, r, nil) if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) return } // defer conn.Close() @@ -270,14 +261,14 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac // 解析目标URL,包含了查询参数 targetURL, err := url.Parse(httpAddress + r.URL.String()) if err != nil { - http.Error(w, "Error parsing target URL", http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusInternalServerError) return } // 创建新的请求 proxyReq, err := http.NewRequest(r.Method, targetURL.String(), r.Body) if err != nil { - http.Error(w, "Error creating proxy request", http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -295,7 +286,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac // 发送请求 resp, err := backend.Client.Do(proxyReq) if err != nil { - http.Error(w, "Error sending proxy request", http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusInternalServerError) return } defer resp.Body.Close() @@ -311,7 +302,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac w.WriteHeader(resp.StatusCode) _, err = io.Copy(w, resp.Body) if err != nil { - http.Error(w, "Error copying proxy response", http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusInternalServerError) return } }