英文:
How to run a specific function each time someone enters my website in go?
问题
我正在使用以下代码:
func main() {
http.Handle("/", http.FileServer(http.Dir("./web-files")))
http.HandleFunc("/auth/verify", verify)
http.HandleFunc("/auth/login", login)
http.HandleFunc("/auth/signup", signup)
http.ListenAndServe(":8080", nil)
}
我是Go语言的新手,我想要的是每当有人进入我的网页时,运行一个名为updateCookies
的函数。
我尝试使用http.HandleFunc("/", updateCookies)
,但它不起作用,因为我已经使用了http.Handle("/", http.FileServer(http.Dir("./web-files")))
谢谢。
英文:
I'm using the following code:
func main() {
http.Handle("/", http.FileServer(http.Dir("./web-files")))
http.HandleFunc("/auth/verify", verify)
http.HandleFunc("/auth/login", login)
http.HandleFunc("/auth/signup", signup)
http.ListenAndServe(":8080", nil)
}
I'm new to go and what I want to do is that every time someone enters my webpage a function named updateCookies
runs.
I've tried to use http.HandleFunc("/", updateCookies)
but it didn't work cause I have already used http.Handle("/", http.FileServer(http.Dir("./web-files")))
Thanks
答案1
得分: 2
你的应用程序的处理程序是http.DefaultServeMux。将该处理程序包装在另一个处理程序中,以执行你的代码:
// wrap 返回一个包装了另一个 http.Handler 的 http.HandlerFunc
func wrap(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Before hook.
updateCookies(w, r)
h.ServeHTTP(w, r) // 调用被包装的处理程序
// After hook.
// 目前为空。
}
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./web-files")))
http.HandleFunc("/auth/verify", verify)
http.HandleFunc("/auth/login", login)
http.HandleFunc("/auth/signup", signup)
http.ListenAndServe(":8080", wrap(http.DefaultServeMux))
}
我编写了包装任意处理程序的代码,因为可以轻松过渡到使用你自己的http.ServeMux:
func main() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("./web-files")))
mux.HandleFunc("/auth/verify", verify)
mux.HandleFunc("/auth/login", login)
mux.HandleFunc("/auth/signup", signup)
http.ListenAndServe(":8080", wrap(mux))
}
任何包都可以在http.DefaultServeMux中注册处理程序。创建自己的mux确保你完全控制在应用程序中运行的处理程序。
英文:
The handler for the your application is http.DefaultServeMux. Wrap that handler with another handler that executes your code:
// wrap returns an http.Handler that wraps another http.Handler
func wrap(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Before hook.
updateCookies(w, r)
h.ServeHTTP(w, r) // call the wrapped handler
// After hook.
// Nothing for now.
}
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./web-files")))
http.HandleFunc("/auth/verify", verify)
http.HandleFunc("/auth/login", login)
http.HandleFunc("/auth/signup", signup)
http.ListenAndServe(":8080", wrap(http.DefaultServeMux))
}
I wrote the code to wrap an arbitrary handler because there's an easy transition to using your own http.ServeMux:
func main() {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("./web-files")))
mux.HandleFunc("/auth/verify", verify)
mux.HandleFunc("/auth/login", login)
mux.HandleFunc("/auth/signup", signup)
http.ListenAndServe(":8080", wrap(mux))
}
Any package can register a handler in http.DefaultServeMux. Creating your own mux ensures that you have complete control over the handlers running in your application.
答案2
得分: 1
http.Handle
、http.HandleFunc
和http.ListenAndServe
(第二个参数为nil
)使用http.DefaultServeMux
来将请求路由到相应的处理程序。
只有当http.ListenAndServe
的第二个参数为nil
时,它才会使用默认的mux。如果提供了非nil
的参数,它将使用该参数来处理传入的请求,而不是使用默认的mux。
考虑到http.ListenAndServe
的第二个参数的类型是http.Handler
接口,你可以简单地将updateCookies
作为第二个参数传递给http.ListenAndServe
(尽管你需要将其显式转换为http.HandlerFunc
),然后在updateCookies
中,在最后将w
和r
传递给默认的mux。
func updateCookies(w http.ResponseWriter, r *http.Request) {
// ... [你的原始cookie代码] ...
http.DefaultServeMux.ServeHTTP(w, r)
}
// ...
http.ListenAndServe(":8080", http.HandlerFunc(updateCookies))
英文:
http.Handle
, http.HandleFunc
, and http.ListenAndServe
(with nil
as the second argument), use http.DefaultServeMux
for routing the requests to their respective handlers.
http.ListenAndServe
will use the default mux ONLY when the second argument passed to it is nil
. If a non-nil argument is provided then it will use that, instead of the default mux, to handle the incoming requests.
Given that http.ListenAndServe
's second parameter's type is the http.Handler
interface, you could simply pass updateCookies
to http.ListenAndServe
as the second argument (though you'll have to convert it explicitly to http.HandlerFunc
) and then modify updateCookies
to, at the end, pass the w
and the r
to the default mux.
func updateCookies(w http.ResponseWriter, r *http.Request) {
// ... [your original cookie code] ...
http.DefaultServeMux.ServeHTTP(w, r)
}
// ...
http.ListenAndServe(":8080", http.HandlerFunc(updateCookies))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论