go - 在 Goji 中映射所有路由及其 http 方法

标签 go routes goji

我想映射每个路由及其请求类型(GET、POST、PUT 等),以便为我的 restful API 生成类似于 JSON 格式的 sitemap.xml。

Goji 使用函数创建新路线。我可以将路径和处理程序存储在 map 中。

我的方法是这样的,除了编译器给出以下初始化循环错误,因为 sitemaproutes 相互引用(routemap 包含处理程序站点地图应该 marhsall 本身)。

main.go:18: initialization loop:
    main.go:18 routes refers to
    main.go:41 sitemap refers to
    main.go:18 routes

这可以用更惯用的方式实现吗?

package main

import (
    "encoding/json"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

var routes = []Route{
    Route{"Get", "/index", hello},
    Route{"Get", "/sitemap", sitemap},
}

type Route struct {
    Method  string          `json:"method"`
    Pattern string          `json:"pattern"`
    Handler web.HandlerType `json:"-"`
}

func NewRoute(method, pattern string, handler web.HandlerType) {
    switch method {
    case "Get", "get":
        goji.DefaultMux.Get(pattern, handler)
    case "Post", "post":
        goji.DefaultMux.Post(pattern, handler)
        // and so on...
    }

}

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world"))
}

func sitemap(c web.C, w http.ResponseWriter, r *http.Request) {
    // BUG: sitemap tries to marshall itself recursively
    resp, _ := json.MarshalIndent(routes, "", "    ")
// some error handling...
    w.Write(resp)
}

func main() {

    for _, r := range routes {
        NewRoute(r.Method, r.Pattern, r.Handler)
    }

    goji.Serve()
}

最佳答案

避免初始化循环的最简单方法是通过延迟其中一项初始化来打破循环。

例如:

var routes []Route

func init() {
    routes = []Route{
        Route{"Get", "/index", hello},
        Route{"Get", "/sitemap", sitemap},
    }
}

通过此更改,您的代码可以编译。

[在 chat 之后编辑:]

下面是一个完全编辑和可运行的示例,它也解决了您关于 switch 的问题:

package main

import (
    "encoding/json"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

var routes []Route

func init() {
    // Initialzed in init() to avoid an initialization loop
    // since `routes` refers to `sitemap` refers to `routes`.
    routes = []Route{
        Route{"Get", "/index", hello},
        Route{"Get", "/sitemap", sitemap},
        //Route{"Post", "/somewhereElse", postHandlerExample},
    }
}

type Route struct {
    Method  string          `json:"method"`
    Pattern string          `json:"pattern"`
    Handler web.HandlerType `json:"-"`
}

var methods = map[string]func(web.PatternType, web.HandlerType){
    "Get":  goji.Get,
    "Post": goji.Post,
    // … others?
}

func (r Route) Add() {
    //log.Println("adding", r)
    methods[r.Method](r.Pattern, r.Handler)
}

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world"))
}

func sitemap(c web.C, w http.ResponseWriter, r *http.Request) {
    resp, err := json.MarshalIndent(routes, "", "    ")
    if err != nil {
        http.Error(w, "Can't generate response properly.", 500)
        return
    }

    w.Write(resp)
}

func main() {
    for _, r := range routes {
        r.Add()
    }
    goji.Serve()
}

可用 gist .

我会注意到,您使用的开关没有任何问题, 在这种情况下,如果只有两种方法,则 map 可能有点矫枉过正。 previous version of the example 没有使用映射并明确指定了函数和方法名称(它们应该匹配)。

此外,此版本不检查无效的方法名称(如果 routes 始终是硬编码并且在运行时从不更改是合理的)。如果需要,可以直接执行 fn, ok := methods[r.Method] 并在 !ok 时执行其他操作。

关于go - 在 Goji 中映射所有路由及其 http 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30374089/

相关文章:

go - 监听 TCP4 而不是 TCP6

Golang 枸杞 : How to serve static content and api at the same time

mongodb - 在 Go 中模拟 MongoDB 响应

ruby-on-rails-3 - 路由中没有父 ID 的嵌套资源

email - 包含 html 内容的邮件显示换行符或忽略换行符

ruby-on-rails - 安装发动机的 RSpec 测试路线

CodeIgniter 路由不起作用

go - 如何在 Goji (Golang) 中使用不同的中间件创建单独的路由组?

go - go-sql-driver Scan() 的命名键

go - 在 Go 中是否可以创建静态或动态库之类的东西,并从两个项目中删除源代码并放在一个中立的地方?