亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 httputil.ReverseProxy 時 mux.Vars 為空

使用 httputil.ReverseProxy 時 mux.Vars 為空

Go
慕少森 2021-10-11 10:39:34
我正在嘗試同時使用 gorilla mux 和 httputil.ReverseProxy,但是在嘗試獲取 mux.Vars 時它是空的。根據https://golang.org/src/net/http/httputil/reverseproxy.go?s=2744:2819#L93似乎 http.Request 指針是原始請求的淺表副本,它應該仍然有效.有任何想法嗎?https://play.golang.org/p/JpjNvEMIFBpackage mainimport (    "github.com/gorilla/mux"    "log"    "net/http"    "net/http/httputil"    "net/url")type route struct {    match string    base  string}var routes = []route{    // proxy http://localhost:3000/api/foo/bar => https://api.bar.com/5/foo/bar    route{match: "/api/{path}", base: "https://api.bar.com/5"},    route{match: "/sales/{path}", base: "https://sales.bar.com/3"},}func NewProxy(r *route) http.Handler {    director := func(req *http.Request) {        out, _ := url.Parse(r.base)        req.URL.Scheme = out.Scheme        req.URL.Host = out.Host        req.URL.Path = out.Path + "/" + mux.Vars(req)["path"] // mux Vars are empty here    }    return &httputil.ReverseProxy{Director: director}}func main() {    for _, route := range routes {        http.Handle(route.match, NewProxy(&route))    }    log.Println("Listening on port 8080")    http.ListenAndServe(":8080", nil)}
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 287 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號