如何将我的AppHandler添加到我的路由中?

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

How do I add my AppHandler to my routes?

问题

我正在尝试按照The Go Blog: Error handling and Go中描述的方式实现一个appHandler
我已经有了appHandler,现在我只是想将其与我的路由连接起来。以下代码可以工作:

router := new(mux.Router)
router.Handle("/my/route/", handlers.AppHandler(handlers.MyRoute))

但是,我想要允许GET请求,并且设置StrictSlash(true)。我有以下代码:

type Routes []Route
type Route struct {
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

var routes = Routes{
    Route{"GET", "/my/route/", handlers.AppHandler(handlers.MyRoute)}
} 
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
    var handler http.Handler
    handler = route.HandlerFunc
    router.Methods(route.Method).Path(route.Pattern).Handler(handler)
}

AppHandler的定义如下:

type AppHandler func(http.ResponseWriter, *http.Request) *appError

func (fn AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // blah, blah, blah
}

我得到了一个错误:

cannot use handlers.AppHandler(handlers.MyRoute) (type handlers.AppHandler) as type http.HandlerFunc in field value
英文:

I am trying to implement an appHandler as described in The Go Blog: Error handling and Go.
I have the appHandler, now I'm just trying to hook it up to my routes. The following works:

router := new(mux.Router)
router.Handle("/my/route/", handlers.AppHandler(handlers.MyRoute))

But, I want to be able to allow GET requests as well as having "StrictSlash(true)". I have:

type Routes []Route
type Route struct {
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}


var routes = Routes{
    Route{"GET", "/my/route/", handlers.AppHandler(handlers.MyRoute)}
} 
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
    var handler http.Handler
	handler = route.HandlerFunc
    router.Methods(route.Method).Path(route.Pattern).Handler(handler)
}

AppHandler looks like:

type AppHandler func(http.ResponseWriter, *http.Request) *appError

func (fn AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// blah, blah, blah
}

I get an error:

cannot use handlers.AppHandler(handlers.MyRoute) (type handlers.AppHandler) as type http.HandlerFunc in field value

答案1

得分: 0

你的AppHandler不是一个http.HandlerFunc,而是一个http.Handler

http.HandlerFunc只是一个接受http.ResponseWriter*http.Request参数的函数。

http.Handler是一个接口,任何具有ServeHTTP(w http.ResponseWriter, r *http.Request)方法的类型都可以满足该接口。

将你的结构体改为类似以下的形式:

type Route struct {
    Method  string
    Pattern string
    Handler http.Handler
}

然后在底部,你可以这样做:

for _, route := range routes {
    router.Methods(route.Method).Path(route.Pattern).Handler(route.Handler)
}
英文:

Your AppHandler isn't a http.HandlerFunc, it's a http.Handler.

A http.HandlerFunc is just a function that takes both a http.ResponseWriter and a *http.Request.

http.Handler is an interface that is satisfied by any type that has a method ServeHTTP(w http.ResponseWriter, r *http.Request)

Change your struct to something like

type Route struct {
    Method  string
    Pattern string
    Handler http.Handler
}

And then at the bottom you can probably do

for _, route := range routes {
    router.Methods(route.Method).Path(route.Pattern).Handler(route.Handler)
}

huangapple
  • 本文由 发表于 2015年5月28日 11:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/30496470.html
匿名

发表评论

匿名网友

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

确定