英文:
HTTP Router and Middleware
问题
我正在使用Julien Schmidt的GoLang路由器,并尝试与Alice一起使用以链接中间件。我遇到了以下错误:
无法将commonHandlers.ThenFunc(final)(类型为http.Handler)作为参数传递给router.GET的类型httprouter.Handle
我不太确定为什么会出现这个错误。
我的代码如下:
package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
)
func middlewareOne(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("执行middlewareOne")
next.ServeHTTP(w, r)
log.Println("再次执行middlewareOne")
})
}
func middlewareTwo(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("执行middlewareTwo")
if r.URL.Path != "/" {
return
}
next.ServeHTTP(w, r)
log.Println("再次执行middlewareTwo")
})
}
func final(w http.ResponseWriter, r *http.Request) {
log.Println("执行finalHandler")
w.Write([]byte("OK"))
}
func main() {
session, err := mgo.Dial("conn-string")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
commonHandlers := alice.New(middlewareOne, middlewareTwo)
router := httprouter.New()
router.GET("/", commonHandlers.ThenFunc(final))
http.ListenAndServe(":5000", router)
}
希望对你有帮助!
英文:
I'm using Julien Schmidt's router for GoLang and trying to get it working with Alice to chain middleware. I'm getting this error:
cannot use commonHandlers.ThenFunc(final) (type http.Handler) as type httprouter.Handle in argument to router.GET
and I'm not quite sure why.
My code is:
package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice"
"gopkg.in/mgo.v2"
//"gopkg.in/mgo.v2/bson"
)
func middlewareOne(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Executing middlewareOne")
next.ServeHTTP(w, r)
log.Println("Executing middlewareOne again")
})
}
func middlewareTwo(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Executing middlewareTwo")
if r.URL.Path != "/" {
return
}
next.ServeHTTP(w, r)
log.Println("Executing middlewareTwo again")
})
}
func final(w http.ResponseWriter, r *http.Request) {
log.Println("Executing finalHandler")
w.Write([]byte("OK"))
}
func main() {
session, err := mgo.Dial("conn-string")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
commonHandlers := alice.New(middlewareOne, middlewareTwo)
router := httprouter.New()
router.GET("/", commonHandlers.ThenFunc(final))
http.ListenAndServe(":5000", router)
}
答案1
得分: 7
httprouter的router.GET
只能与httprouter.Handle
类型一起使用。请使用http.Handler
的Handler
方法:
router.Handler("GET", "/", commonHandlers.ThenFunc(final))
英文:
httprouter's router.GET
works only with httprouter.Handle
type. Use the Handler
method with http.Handler
:
router.Handler("GET", "/", commonHandlers.ThenFunc(final))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论