Gorilla mux是一个HTTP路由器和调度器的包。

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

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

huangapple
  • 本文由 发表于 2017年2月3日 00:05:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/42006921.html
匿名

发表评论

匿名网友

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

确定