修复proxy不传递query参数的问题

This commit is contained in:
eson
2023-07-18 13:04:29 +08:00
parent 125de6dde8
commit e4e9339071
9 changed files with 77 additions and 65 deletions

View File

@@ -75,10 +75,12 @@ func main() {
fs := http.FileServer(http.Dir(vueBuild))
indexHtmlPath := vueBuild + "/index.html"
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api") {
if strings.HasPrefix(r.URL.Path, "/api/") {
// 对/api开头的请求进行反向代理
proxy := httputil.NewSingleHostReverseProxy(apiURL)
proxy.ServeHTTP(w, r)
return
} else {
// 根据请求路径判断是服务静态文件或者是返回index.html
idx := strings.Index(r.URL.Path[1:], "/")
@@ -144,7 +146,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
// 创建处理请求的函数
handleRequest := func(w http.ResponseWriter, r *http.Request) {
// 解析目标URL包含了查询参数
targetURL, err := url.Parse(httpAddress + r.URL.Path)
targetURL, err := url.Parse(httpAddress + r.URL.String())
if err != nil {
http.Error(w, "Error parsing target URL", http.StatusInternalServerError)
return