英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论