dounayan3643 2018-06-04 10:54 采纳率: 0%
浏览 525
已采纳

在golang http中动态添加路由

I am trying to read routes from json and appending them to mux , but instead of appending it seems to overwrite routes.

Code snippet

 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, "
")
        parsedURL, _ := url.Parse(forward)
        mux.Handle(incomming, httputil.NewSingleHostReverseProxy(parsedURL))
}
  • 写回答

1条回答 默认 最新

  • dongsao8279 2018-06-04 13:46
    关注
    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制