英文:
Gorilla mux http package
问题
我正在使用Gorilla mux作为我的Golang应用程序中的路由器和调度程序,并且我有一个 - 我认为 - 简单的问题:
在我的main函数中,我创建了一个路由器:r := mux.NewRouter()。几行之后,我注册了一个处理程序:r.HandleFunc("/", doSomething)。
到目前为止都很好,但是现在我的问题是,我有一个包,它将处理程序添加到Golang的http包而不是我的mux路由器。就像这样:
func AddInternalHandlers() {
    http.HandleFunc("/internal/selfdiagnose.html", handleSelfdiagnose)
    http.HandleFunc("/internal/selfdiagnose.xml", handleSelfdiagnose)
    http.HandleFunc("/internal/selfdiagnose.json", handleSelfdiagnose)
}
如你所见,它将处理程序添加到http.HandleFunc而不是mux.HandleFunc。有没有办法在不修改包本身的情况下解决这个问题?
工作示例
package main
import (
    "fmt"
    "log"
    "net/http"
    selfdiagnose "github.com/emicklei/go-selfdiagnose"
    "github.com/gorilla/mux"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
    log.Println("home")
}
func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", homeHandler)
    selfdiagnose.AddInternalHandlers()
    // 当将处理程序(nil)替换为mux(r)时,
    // 包selfdiagnose中的处理程序将不会“激活”
    err := http.ListenAndServe(fmt.Sprintf("%s:%d", "localhost", 8080), nil)
    if err != nil {
        log.Println(err)
    }
}
英文:
I'm using Gorilla mux as my router and dispatcher in my golang apps and I have a -I think- simple question:
In my main, I create a router: r := mux.NewRouter(). A few lines further, I register a handler: r.HandleFunc("/", doSomething).
So far so good, but now my problem is that I have a package which adds handlers to the http package of Golang and not to my mux router. Like this:
func AddInternalHandlers() {
	http.HandleFunc("/internal/selfdiagnose.html", handleSelfdiagnose)
	http.HandleFunc("/internal/selfdiagnose.xml", handleSelfdiagnose)
	http.HandleFunc("/internal/selfdiagnose.json", handleSelfdiagnose)
}
As you can see, it adds handles to the http.HandleFunc and not to the mux-handleFunc. Any idea how I can fix this without touching the package itself?
Working example
package main
import (
	"fmt"
	"log"
	"net/http"
	selfdiagnose "github.com/emicklei/go-selfdiagnose"
	"github.com/gorilla/mux"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("home")
}
func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", homeHandler)
	selfdiagnose.AddInternalHandlers()
  // when handler (nil) gets replaced with mux (r), then the
  // handlers in package selfdiagnose are not "active"
	err := http.ListenAndServe(fmt.Sprintf("%s:%d", "localhost", 8080), nil)
	if err != nil {
		log.Println(err)
	}
}
答案1
得分: 1
好的,以下是翻译好的内容:
嗯,在你的情况下,解决方案很简单。
selfdiagnose包的作者选择将handlers本身设为公开,所以你可以直接使用它们:
r.HandleFunc("/", homeHandler)
// 直接使用handlers,但你需要自己命名一个路由
r.HandleFunc("/debug", selfdiagnose.HandleSelfdiagnose)
工作示例:https://gist.github.com/miku/9836026cacc170ad5bf7530a75fec777
英文:
Well, in your particular case, the solution is easy.
The author of the selfdiagnose package choose to make handlers themselves public, so you can just use them directly:
r.HandleFunc("/", homeHandler)
// use the handlers directly, but you need to name a route yourself
r.HandleFunc("/debug", selfdiagnose.HandleSelfdiagnose)
Working example: https://gist.github.com/miku/9836026cacc170ad5bf7530a75fec777
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论