Golang路由不起作用

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

Golang route not working

问题

我刚刚开始学习Go语言,并计划托管至少两个网站,所以我选择使用Mux来通过“过滤”域名来显示不同的路由。每当我尝试访问我的主路由时,它都会给我一个404错误。(另外,“www”部分不存在是完全正常的。我不需要输入它来访问网站)。

但是,如果我将服务器作为文件服务器启动,我可以访问我的文件,所以我猜服务器本身是正常工作的。

func redirect(w http.ResponseWriter, req *http.Request) {
    target := "https://" + req.Host + req.URL.Path
    http.Redirect(w, req, target, http.StatusTemporaryRedirect)
}

func main() {
    go http.ListenAndServe(":80", http.HandlerFunc(redirect)) // 重定向

    // 安全服务器

    r := mux.NewRouter()
    r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("/root/go/src/web/static/"))))
    s := r.Host("echecderi.me").Subrouter()
    s.HandleFunc("/", indexEchec)

    http.ListenAndServeTLS(":443", "domain-crt.pem", "domain-key.pem", nil)
}

func indexEchec(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>Echec de rime</h1> </br> <img src=\"/static/echecderime/echec.gif\">")
}
英文:

I just started to get into golang and as I plan to host at least two websites, I chose to use Mux to display different routes by "filtering" domains. Whenever I try to access my main route, it just gives me an 404 error. (Also, the fact that the "www" part is absent is perfectly normal. I don't type that to access the site).

But if I launch the server as a file server, I can access my files, so the server in itself is working I guess

func redirect(w http.ResponseWriter, req *http.Request) {
	target := &quot;https://&quot; + req.Host + req.URL.Path
	http.Redirect(w, req, target,
		http.StatusTemporaryRedirect)
}

func main() {
	go http.ListenAndServe(&quot;:80&quot;, http.HandlerFunc(redirect)) // Redirection

	// Serveur s&#233;curis&#233;

	r := mux.NewRouter()
	r.PathPrefix(&quot;/static/&quot;).Handler(http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;/root/go/src/web/static/&quot;))))
	s := r.Host(&quot;echecderi.me&quot;).Subrouter()
	s.HandleFunc(&quot;/&quot;, indexEchec)

	http.ListenAndServeTLS(&quot;:443&quot;, &quot;domain-crt.pem&quot;, &quot;domain-key.pem&quot;, nil)
}

func indexEchec(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, &quot;&lt;h1&gt;Echec de rime&lt;/h1&gt; &lt;/br&gt; &lt;img src=\&quot;/static/echecderime/echec.gif\&quot;&gt;&quot;)
}

答案1

得分: 2

我认为你需要将r作为最后一个参数传递给http.ListenAndServeTLS函数。

英文:

I think you need to give r as the last parameter to http.ListenAndServeTLS.

答案2

得分: 0

你也可以使用一个 http.server 实例。

// 创建服务器实例
server := http.Server{
    Addr:      ":443",
    TLSConfig: tlsConfig(cert),
}

rtr := mux.NewRouter()
rtr.HandleFunc("/profile", HandlerProfile).Methods("GET")
// rtr.HandleFunc(其他路由...

// 将 mux 处理程序传递给服务器
server.Handler = rtr
server.ListenAndServeTLS("", "")
英文:

you can also use a http.server instance

//create server instance
server := http.Server{
	Addr:      &quot;:443&quot;,
	TLSConfig: tlsConfig(cert),
}

rtr := mux.NewRouter()
rtr.HandleFunc(&quot;/profile&quot;, HandlerProfile).Methods(&quot;GET&quot;)
//rtr.HandleFunc( other routes...

//pass mux handler to server
server.Handler = rtr
server.ListenAndServeTLS(&quot;&quot;, &quot;&quot;)

huangapple
  • 本文由 发表于 2017年3月31日 02:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/43126301.html
匿名

发表评论

匿名网友

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

确定