dongseshu0698
2017-03-29 16:53
浏览 65

路由的Golang JSON配置

I have been trying to setup a JSON config file to setup dynamic routes for my application. The idea is that I will be able to setup my own URL structure depending on who is using the service. I have a struct that takes in JSON and that works fine. I am using gorilla mux.

 type CustomRoute struct {
    Name string
    Method string
    Path string
    HandleFunc string
 }

The JSON is basically identical to the struct and it goes in fine.

The issue I have is getting the HandleFunc section.

Here is the code:

func NewRouter() *mux.Router {

routerInstance := mux.NewRouter().StrictSlash(true)

    /*
    All routes from the routing table
    */

    // r = []CustomRoute with the JSON data 
    r := loadRoute()
    for _, route := range r {
       var handler http.Handler

       handler = route.HandlerFunc
       handler = core.Logger(handler, route.Name)

       routerInstance.
           Methods(route.Method).
           Path(route.Path).
           Name(route.Name).
           Handler(handler)

    }

    return routerInstance
}

I always get the following error (as one would expect)

cannot use route.HandlerFunc (type string) as type http.Handler in assignment: string does not implement http.Handler (missing ServeHTTP method)

I was told to use something like:

var functions = map[string]interface{}{
    "HandleFunc1": HandleFunc1,
}

But I have no idea how to make this work

图片转代码服务由CSDN问答提供 功能建议

我一直在尝试设置JSON配置文件来为我的应用程序设置动态路由。 我的想法是,我将能够根据谁使用该服务来设置自己的URL结构。 我有一个采用JSON的结构,并且工作正常。 我正在使用大猩猩多路复用器。

 类型CustomRoute结构{
名称字符串
方法字符串
路径字符串
 HandleFunc字符串
} 
  <  / pre> 
 
 

JSON与该结构基本相同,并且可以正常使用。

我遇到的问题是获取HandleFunc节。 \ n

这里是代码:

  func NewRouter()* mux.Router {
 
routerInstance:= mux.NewRouter()。StrictSlash(true)  
 
 / * 
路由表中的所有路由
 * / 
 
 // r = []带有JSON数据的CustomRoute 
r:= loadRoute()
表示_,route:= range r  {
 var handler http.Handler 
 
 handler = route.HandlerFunc 
 handler = core.Logger(handler,route.Name)
 
 routerInstance。
 Methods(route.Method)。
 Path(  route.Path)。
名称(route.Name)。
处理程序(处理程序)
 
} 
 
返回routerInstance 
} 
   
 
 

我总是会收到以下错误(正如人们期望的那样)

不能将route.HandlerFunc(类型字符串)用作分配中的http.Handler类型: 字符串未实现http.Handler(缺少ServeHTTP方法)

有人告诉我使用类似的东西:

  var functions = map [string] interface {} {
“ HandleFunc1”:HandleFunc1,
} 
   
 
 

但是我不知道如何使这项工作

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchu7271 2017-03-29 17:21
    已采纳

    Thanks to RayenWindspear I was able to fix the problem. It was very simple (like everything). The map code should look like this:

    var functions = map[string]http.HandlerFunc{
        "HandleFunc1": HandleFunc1,
    }
    
    已采纳该答案
    打赏 评论
  • dousu1916 2017-03-29 17:09

    I am using a mux for subdomains, so my examples may be a bit off. The map you are being told to use is done something like this:

    type Handlers map[string]http.HandlerFunc
    
    func (handlers Handlers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        path := r.URL.Path
        if handle := handlers[path]; handle != nil {
            handle.ServeHTTP(w, r)
        } else {
            http.Error(w, "Not found", 404)
        }
    }
    
    打赏 评论

相关推荐 更多相似问题