一个常见的HTTP处理程序而不是几个不同的处理程序。

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

one common http handler instead of several

问题

在这段代码中,你想知道是否有可能不复制粘贴表达式commonHanlder(handler1)commonHanlder(handler2)... commonHanlder(handlerN),而是在一个地方设置它,例如:

rtr.HandleFunc("/", commonHanlder(handler1)).Methods("GET")
rtr.HandleFunc("/page2", commonHanlder(handler2)).Methods("GET")

并将其设置在一个地方,如:

http.ListenAndServe(":3000", commonHanlder(http.DefaultServeMux))

但是这种变体不起作用,并在编译时产生两个错误:

./goRelicAndMux.go:20: cannot use http.DefaultServeMux (type *http.ServeMux) as type gorelic.tHTTPHandlerFunc in argument to commonHanlder
./goRelicAndMux.go:20: cannot use commonHanlder(http.DefaultServeMux) (type gorelic.tHTTPHandlerFunc) as type http.Handler in argument to http.ListenAndServe:
    gorelic.tHTTPHandlerFunc does not implement http.Handler (missing ServeHTTP method)

完整的代码如下:

package main

import (
	"github.com/gorilla/mux"
	"github.com/yvasiyarov/gorelic"
	"log"
	"net/http"
)

func main() {
	initNewRelic()
	rtr := mux.NewRouter()
	var commonHanlder = agent.WrapHTTPHandlerFunc

	rtr.HandleFunc("/", commonHanlder(handler1)).Methods("GET")
	rtr.HandleFunc("/page2", commonHanlder(handler2)).Methods("GET")

	http.Handle("/", rtr)
	log.Println("Listening...")
	http.ListenAndServe(":3000", http.DefaultServeMux)
}

func handler1(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("mainPage"))
}

func handler2(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("page 2"))
}

var agent *gorelic.Agent

func initNewRelic() {
	agent = gorelic.NewAgent()
	agent.Verbose = true
	agent.NewrelicName = "test"
	agent.NewrelicLicense = "new relic key"
	agent.Run()
}

请注意,根据错误信息,commonHanlder的类型是gorelic.tHTTPHandlerFunc,而不是http.Handler。因此,无法将commonHanlder(http.DefaultServeMux)作为参数传递给http.ListenAndServe函数。这是因为gorelic.tHTTPHandlerFunc类型没有实现http.Handler接口的ServeHTTP方法。

如果你想在一个地方设置commonHanlder,你需要确保commonHanlder的类型是http.Handler。你可以通过将commonHanlder的类型更改为http.Handler来解决这个问题。例如:

var commonHanlder http.Handler = agent.WrapHTTPHandlerFunc

然后,你可以使用commonHanlder作为参数传递给http.ListenAndServe函数:

http.ListenAndServe(":3000", commonHanlder)

这样就不会产生编译错误了。希望这可以帮助到你!

英文:

Is it possible to not copy paste expression commonHanlder(handler1), commonHanlder(handler2) ... commonHanlder(handlerN) in this code:

rtr.HandleFunc("/", commonHanlder(handler1)).Methods("GET")
rtr.HandleFunc("/page2", commonHanlder(handler2)).Methods("GET")

and set it in one place like

http.ListenAndServe(":3000", commonHanlder(http.DefaultServeMux))

But this variant is not working and gives two errors on compile:

./goRelicAndMux.go:20: cannot use http.DefaultServeMux (type *http.ServeMux) as type gorelic.tHTTPHandlerFunc in argument to commonHanlder
./goRelicAndMux.go:20: cannot use commonHanlder(http.DefaultServeMux) (type gorelic.tHTTPHandlerFunc) as type http.Handler in argument to http.ListenAndServe:
    gorelic.tHTTPHandlerFunc does not implement http.Handler (missing ServeHTTP method)

The full code:

package main

import (
	"github.com/gorilla/mux"
	"github.com/yvasiyarov/gorelic"
	"log"
	"net/http"
)

func main() {
	initNewRelic()
	rtr := mux.NewRouter()
	var commonHanlder = agent.WrapHTTPHandlerFunc

	rtr.HandleFunc("/", commonHanlder(handler1)).Methods("GET")
	rtr.HandleFunc("/page2", commonHanlder(handler2)).Methods("GET")

	http.Handle("/", rtr)
	log.Println("Listening...")
	http.ListenAndServe(":3000", http.DefaultServeMux)
}

func handler1(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("mainPage"))
}

func handler2(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("page 2"))
}

var agent *gorelic.Agent

func initNewRelic() {
	agent = gorelic.NewAgent()
	agent.Verbose = true
	agent.NewrelicName = "test"
	agent.NewrelicLicense = "new relic key"
	agent.Run()
}

答案1

得分: 1

看起来你想在应用程序的根目录上调用commonHandler,并使其适用于所有情况。由于你正在使用mux,只需将mux路由器包装一次即可。

func main() {
    initNewRelic()
    rtr := mux.NewRouter()
    var commonHandler = agent.WrapHTTPHandler

    rtr.HandleFunc("/", handler1).Methods("GET")
    rtr.HandleFunc("/page2", handler2).Methods("GET")

    http.Handle("/", commonHandler(rtr))
    log.Println("Listening...")
    http.ListenAndServe(":3000", nil)
}

我还删除了ListenAndServe中的http.DefaultServeMux引用,因为传递nil将自动使用默认值。

英文:

It seems like you want to call commonHandler on the root of your application and have it work for all. Since you are using mux, just wrap the mux router once.

func main() {
    initNewRelic()
    rtr := mux.NewRouter()
    var commonHandler = agent.WrapHTTPHandler

    rtr.HandleFunc("/", handler1).Methods("GET")
    rtr.HandleFunc("/page2", handler2).Methods("GET")

    http.Handle("/", commonHandler(rtr))
    log.Println("Listening...")
    http.ListenAndServe(":3000", nil)
}

I also removed the http.DefaultServeMux reference in ListenAndServe since passing nil will automatically use the default.

huangapple
  • 本文由 发表于 2015年2月28日 12:04:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/28777972.html
匿名

发表评论

匿名网友

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

确定