Custom 404 error message with Go

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

Custom 404 error message with Go

问题

我有这样一段代码,如果我访问/time/加上任何内容,它将显示自定义的404错误消息,但如果我访问/times/、/或/whatever,它将显示默认的404错误消息。我想在除了/time/之外的所有情况下都显示自定义的404页面。

package main

import (
	"fmt"
	"time"
	"flag"
	"os"
	"net/http"
)

const AppVersion = "timeserver version: 3.0"

func timeserver(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/time/" {
		NotFoundHandler(w, r)
		return
	}

	const layout = "3:04:05 PM"
	t := time.Now().Local()
	fmt.Fprint(w, "<html>\n")
	fmt.Fprint(w, "<head>\n")
	fmt.Fprint(w, "<style>\n")
	fmt.Fprint(w, "p {font-size: xx-large}\n")
	fmt.Fprint(w, "span.time {color: red}\n")
	fmt.Fprint(w, "</style>\n")
	fmt.Fprint(w, "</head>\n")
	fmt.Fprint(w, "<body>\n")
	//fmt.Fprint(w, "The time is now " + t.Format(layout))
	fmt.Fprint(w, "<p>The time is now <span class=\"time\">"+ t.Format(layout) +"</span>.</p>\n")
	fmt.Fprint(w, "</body>\n")
	fmt.Fprint(w, "</html>\n")
}

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "custom 404")
}

func main() {
	version := flag.Bool("V", false, "prints current version")
	port := flag.String("port", "8080", "sets service port number")

	flag.Parse()
	if *version {
		fmt.Println(AppVersion)
		os.Exit(0)
	}

	http.HandleFunc("/time/", timeserver)
	http.ListenAndServe("localhost:"+ *port, nil)
}

请问有什么我可以帮助你的吗?

英文:

I have this code that will give my custom 404 error message if I go to /time/<anything here will give custom 404> but if I go to /times/ or just / or /whatever then I will get the default 404 error message. I want to show my custom 404 for all cases other than /time/

package main
import (
&quot;fmt&quot;
&quot;time&quot;
&quot;flag&quot;
&quot;os&quot;
&quot;net/http&quot;
)
const AppVersion = &quot;timeserver version: 3.0&quot;
func timeserver(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != &quot;/time/&quot; {
NotFoundHandler(w, r)
return
}
const layout = &quot;3:04:05 PM&quot;
t := time.Now().Local()
fmt.Fprint(w, &quot;&lt;html&gt;\n&quot;)
fmt.Fprint(w, &quot;&lt;head&gt;\n&quot;)
fmt.Fprint(w, &quot;&lt;style&gt;\n&quot;)
fmt.Fprint(w, &quot;p {font-size: xx-large}\n&quot;)
fmt.Fprint(w, &quot;span.time {color: red}\n&quot;)
fmt.Fprint(w, &quot;&lt;/style&gt;\n&quot;)
fmt.Fprint(w, &quot;&lt;/head&gt;\n&quot;)
fmt.Fprint(w, &quot;&lt;body&gt;\n&quot;)
//fmt.Fprint(w, &quot;The time is now &quot; + t.Format(layout))
fmt.Fprint(w, &quot;&lt;p&gt;The time is now &lt;span class=\&quot;time\&quot;&gt;&quot; + t.Format(layout) + &quot;&lt;/span&gt;.&lt;/p&gt;\n&quot;)
fmt.Fprint(w, &quot;&lt;/body&gt;\n&quot;)
fmt.Fprint(w, &quot;&lt;/html&gt;\n&quot;)
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, &quot;custom 404&quot;)
}
func main() {
version := flag.Bool(&quot;V&quot;, false, &quot;prints current version&quot;)
port := flag.String(&quot;port&quot;, &quot;8080&quot;, &quot;sets service port number&quot;)
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
http.HandleFunc(&quot;/time/&quot;, timeserver)
http.ListenAndServe(&quot;localhost:&quot; + *port, nil)
}

答案1

得分: 7

将以下内容添加到主函数中:

http.HandleFunc("/", NotFoundHandler)

"/" 的处理程序是通用处理程序。

此外,你应该修改处理程序以返回 404 状态:

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
   w.WriteHeader(http.StatusNotFound)
   fmt.Fprint(w, "custom 404")
}
英文:

Add this line to main:

http.HandleFunc(&quot;/&quot;, NotFoundHandler)

The handler for "/" is the catchall handler.

Also, you should modify the handler to return a 404 status:

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, &quot;custom 404&quot;)
}

huangapple
  • 本文由 发表于 2015年1月13日 07:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/27912409.html
匿名

发表评论

匿名网友

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

确定