How to redirect URL in Go main method?

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

How to redirect URL in Go main method?

问题

我在Go语言中设置了一个GorrilaMux,如果在浏览器中输入特定的URL,它将进行API调用。如果URL作为命令行参数给出,我希望在我的主方法中进行相同的API调用。然而,似乎可以做到这一点的http.redirect()方法需要一个HTTP ResponseWriter和一个*HTTPRequest变量作为函数参数。我不知道如何在主方法中生成这些变量。我该如何做,或者在Golang中有更好的方法从URL进行API调用?

设置路由的代码如下:

func main(){
   router := mux.NewRouter().StrictSlash(true)
   for _, route := range routes { //设置预定义路由
     router.
        Path(route.Path).
        Name(route.Name).
        Handler(route.HandlerFunc)
    }

  URL := "localhost:8080/whatever" //我想要重定向的URL,路由将是"/whatever"

 http.redirect(????)

 }

请注意,我只会翻译代码部分,不会回答关于翻译的问题。

英文:

I have a GorrilaMux set up in Go that will make an API call if type in a specific URL in a browser. I want to make the same API call in my main method in go if the URL is given as a command line argument. However, the http.redirect() method which seems to be able to do this, requires a HTTP ResponseWriter and a *HTTPRequest variables as function parameters. I do not know how to produce these variables within a main method. How do I do this, OR, is there a better way to make the API call from the URL in Golang?

Code to set up Router

func main(){
   router := mux.NewRouter().StrictSlash(true)
   for _, route := range routes { //Sets up predefined routes
     router.
		Path(route.Path).
		Name(route.Name).
		Handler(route.HandlerFunc)
    }

  URL:="localhost:8080/whatever" //URL I want to redirect, route would be "/whatever"

 http.redirect(????)

 }

答案1

得分: 2

HTTP重定向是对客户端的响应,应该从处理程序的调用中调用。在主函数的上下文中,http.redirect(w http.ResponseWriter, r *http.Request)函数没有意义。

您可以像这样为给定的路由注册处理程序:

router.Path("/whatever").Handler(func(writer http.ResponseWriter, req *http.Request) {
    http.Redirect(writer, req, "localhost:8080/whatever", http.StatusMovedPermanently)
}))

这将向路由器添加一个路径,并调用简单的http.Handlerfunc,其中包含对http.Redirect(...)的调用。在这里,这是有意义的,因为我们正在处理对客户端连接的响应。返回301状态代码和重定向目标的URL是合乎逻辑的。

英文:

An HTTP redirect is a response to a client and should be called from an invocation of a handler. The http.redirect(w http.ResponseWriter, r *http.Request) function has no meaning in the context of the main function.

You can register a handler for the given route like so:

router.Path("/whatever").Handler(func(writer http.ResponseWriter, req *http.Request) {
    http.Redirect(writer, req, "localhost:8080/whatever", http.StatusMovedPermanently)
))

This adds a path to the router and invokes the simple http.Handlerfunc which contains a call to http.Redirect(...). Here this makes sense because we are handling a response to a client connection. It is logical to return a 301 status code and the URL for the target of the redirect.

答案2

得分: 0

我们使用一个辅助函数来使代码更清晰。


// Redirect函数将执行HTTP重定向到给定的重定向路径。
func Redirect(redirectPath string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, redirectPath, http.StatusFound)
	}
}


router.Path("/whatever").Handler(Redirect("/test_path"))
router.Path("/whatever2").Handler(Redirect("/test_path2"))
英文:

We use a helper function to make the code cleaner.


// Redirect will peform an HTTP redirect to the given redirect Path.
func Redirect(redirectPath string) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, redirectPath, http.StatusFound)
	}
}


router.Path("/whatever").Handler(Redirect("/test_path"))
router.Path("/whatever2").Handler(Redirect("/test_path2"))

huangapple
  • 本文由 发表于 2015年10月23日 08:38:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/33293072.html
匿名

发表评论

匿名网友

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

确定