proxyserver
This commit is contained in:
		
							parent
							
								
									fc50bf587f
								
							
						
					
					
						commit
						e29e8d8049
					
				@ -104,18 +104,8 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			// 对/api开头的请求进行反向代理
 | 
								// 对/api开头的请求进行反向代理
 | 
				
			||||||
			proxy := httputil.NewSingleHostReverseProxy(apiURL)
 | 
								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)
 | 
								proxy.ServeHTTP(w, r)
 | 
				
			||||||
			log.Println(w.Header())
 | 
					
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
@ -258,6 +248,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			conn, err := upgrader.Upgrade(w, r, nil)
 | 
								conn, err := upgrader.Upgrade(w, r, nil)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
 | 
									http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// defer conn.Close()
 | 
								// defer conn.Close()
 | 
				
			||||||
@ -270,14 +261,14 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
 | 
				
			|||||||
		// 解析目标URL,包含了查询参数
 | 
							// 解析目标URL,包含了查询参数
 | 
				
			||||||
		targetURL, err := url.Parse(httpAddress + r.URL.String())
 | 
							targetURL, err := url.Parse(httpAddress + r.URL.String())
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			http.Error(w, "Error parsing target URL", http.StatusInternalServerError)
 | 
								http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 创建新的请求
 | 
							// 创建新的请求
 | 
				
			||||||
		proxyReq, err := http.NewRequest(r.Method, targetURL.String(), r.Body)
 | 
							proxyReq, err := http.NewRequest(r.Method, targetURL.String(), r.Body)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
 | 
								http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -295,7 +286,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
 | 
				
			|||||||
		// 发送请求
 | 
							// 发送请求
 | 
				
			||||||
		resp, err := backend.Client.Do(proxyReq)
 | 
							resp, err := backend.Client.Do(proxyReq)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			http.Error(w, "Error sending proxy request", http.StatusInternalServerError)
 | 
								http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		defer resp.Body.Close()
 | 
							defer resp.Body.Close()
 | 
				
			||||||
@ -311,7 +302,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
 | 
				
			|||||||
		w.WriteHeader(resp.StatusCode)
 | 
							w.WriteHeader(resp.StatusCode)
 | 
				
			||||||
		_, err = io.Copy(w, resp.Body)
 | 
							_, err = io.Copy(w, resp.Body)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			http.Error(w, "Error copying proxy response", http.StatusInternalServerError)
 | 
								http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user