HTTP路由器和中间件

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

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.HandlerHandler方法:

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

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

发表评论

匿名网友

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

确定