gpt4 book ai didi

go - golang http动态添加路由

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:44 28 4
gpt4 key购买 nike

我正在尝试从 json 读取路由并将它们附加到 mux ,但不是附加它似乎覆盖了路由。

代码片段

 func main() {

configLoader()

dispatchMux := http.NewServeMux()

for _, site := range ServerConfigData.Sites {
append(dispatchMux, site.Incomming, site.Forward)
}

// Start the server

color.Green("Server Spinned Up on 2096")
http.ListenAndServe(":2096", dispatchMux)
}

// Route Appender
func append(mux *http.ServeMux, incomming, forward string) {
color.Yellow("Starting ", incomming, " on ", forward, "\n")
parsedURL, _ := url.Parse(forward)
mux.Handle(incomming, httputil.NewSingleHostReverseProxy(parsedURL))
}

最佳答案

func main() {

configLoader()

dispatchMux := http.NewServeMux()

for _, site := range ServerConfigData.Sites {

//Previously code of append function
parsedURL, _ := url.Parse(site.Forward)
fmt.Println(site.Incomming)
dispatchMux.Handle(site.Incomming, httputil.NewSingleHostReverseProxy(parsedURL))

}
// Start the server
color.Green("Server Spinned Up on 2096")
http.ListenAndServe(":80", dispatchMux)
}

关于go - golang http动态添加路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678949/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com