新增自定义的 Cors

This commit is contained in:
eson
2023-07-11 19:43:46 +08:00
parent e0de8bfcf2
commit a4f429a528
17 changed files with 88 additions and 21 deletions

View File

@@ -30,7 +30,7 @@ func SetCors(w http.ResponseWriter, r *http.Request) {
func main() {
rootDir := "../server" // Change this to your root directory
vueBuild := "/home/ldm/workdir/fusenpack-vue-created"
vueBuild := "/home/eson/workspace/fusenpack-vue-created"
apiURL, err := url.Parse("http://localhost:9900")
if err != nil {
panic(err)
@@ -130,7 +130,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
return
}
hasBridge := strings.Contains(r.Header.Get("Access-Control-Request-Headers"), "bridge")
// hasBridge := strings.Contains(r.Header.Get("Access-Control-Request-Headers"), "bridge")
// 解析目标URL时已经包含了查询参数
targetURL.RawQuery = r.URL.RawQuery
@@ -166,9 +166,9 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
}
}
if hasBridge {
w.Header().Add("Access-Control-Allow-Headers", "Bridge")
}
// if hasBridge {
// w.Header().Add("Access-Control-Allow-Headers", "Bridge")
// }
// 转发目标服务器的响应状态码和主体
w.WriteHeader(resp.StatusCode)
@@ -177,8 +177,8 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
http.Error(w, "Error copying proxy response", http.StatusInternalServerError)
return
}
}
for _, muxPath := range muxPaths {
mux.HandleFunc(muxPath, handleRequest)
}