Golang的JSON路由配置

huangapple go评论94阅读模式
英文:

Golang JSON config for routes

问题

我一直在尝试设置一个JSON配置文件来为我的应用程序设置动态路由。我的想法是,根据使用该服务的人,我将能够设置自己的URL结构。我有一个接受JSON的结构体,它工作得很好。我正在使用gorilla mux。

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

JSON基本上与结构体相同,并且可以成功加载。

我遇到的问题是获取HandleFunc部分。

以下是代码:

func NewRouter() *mux.Router {
    routerInstance := mux.NewRouter().StrictSlash(true)

    /*
    所有来自路由表的路由
    */

    // 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
}

我总是得到以下错误(正如人们所预期的):

无法将route.HandlerFunc(类型为string)分配给类型http.Handler:
string未实现http.Handler(缺少ServeHTTP方法)

有人告诉我使用类似以下的代码:

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

但我不知道如何使其工作。

英文:

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

答案1

得分: 1

我正在使用一个多路复用器(mux)来处理子域名,所以我的示例可能有点不准确。你被告知要使用的映射(map)是这样实现的:

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)
    }
}

这段代码定义了一个名为Handlers的类型,它是一个映射,将字符串路径映射到http.HandlerFunc类型的处理函数。ServeHTTP方法用于处理HTTP请求,它首先获取请求的路径,然后检查映射中是否存在对应的处理函数。如果存在,则调用该处理函数的ServeHTTP方法处理请求;如果不存在,则返回一个404错误。

英文:

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)
	}
}

答案2

得分: 1

感谢RayenWindspear,我成功解决了这个问题。问题非常简单(就像一切一样)。地图代码应该像这样:

var functions = map[string]http.HandlerFunc{
    "HandleFunc1": HandleFunc1,
}
英文:

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,
}

huangapple
  • 本文由 发表于 2017年3月30日 00:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/43099629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定