Custom 404 error message with Go

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

Custom 404 error message with Go

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "flag"
  6. "os"
  7. "net/http"
  8. )
  9. const AppVersion = "timeserver version: 3.0"
  10. func timeserver(w http.ResponseWriter, r *http.Request) {
  11. if r.URL.Path != "/time/" {
  12. NotFoundHandler(w, r)
  13. return
  14. }
  15. const layout = "3:04:05 PM"
  16. t := time.Now().Local()
  17. fmt.Fprint(w, "<html>\n")
  18. fmt.Fprint(w, "<head>\n")
  19. fmt.Fprint(w, "<style>\n")
  20. fmt.Fprint(w, "p {font-size: xx-large}\n")
  21. fmt.Fprint(w, "span.time {color: red}\n")
  22. fmt.Fprint(w, "</style>\n")
  23. fmt.Fprint(w, "</head>\n")
  24. fmt.Fprint(w, "<body>\n")
  25. //fmt.Fprint(w, "The time is now " + t.Format(layout))
  26. fmt.Fprint(w, "<p>The time is now <span class=\"time\">"+ t.Format(layout) +"</span>.</p>\n")
  27. fmt.Fprint(w, "</body>\n")
  28. fmt.Fprint(w, "</html>\n")
  29. }
  30. func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
  31. fmt.Fprint(w, "custom 404")
  32. }
  33. func main() {
  34. version := flag.Bool("V", false, "prints current version")
  35. port := flag.String("port", "8080", "sets service port number")
  36. flag.Parse()
  37. if *version {
  38. fmt.Println(AppVersion)
  39. os.Exit(0)
  40. }
  41. http.HandleFunc("/time/", timeserver)
  42. http.ListenAndServe("localhost:"+ *port, nil)
  43. }

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

英文:

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/

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;time&quot;
  5. &quot;flag&quot;
  6. &quot;os&quot;
  7. &quot;net/http&quot;
  8. )
  9. const AppVersion = &quot;timeserver version: 3.0&quot;
  10. func timeserver(w http.ResponseWriter, r *http.Request) {
  11. if r.URL.Path != &quot;/time/&quot; {
  12. NotFoundHandler(w, r)
  13. return
  14. }
  15. const layout = &quot;3:04:05 PM&quot;
  16. t := time.Now().Local()
  17. fmt.Fprint(w, &quot;&lt;html&gt;\n&quot;)
  18. fmt.Fprint(w, &quot;&lt;head&gt;\n&quot;)
  19. fmt.Fprint(w, &quot;&lt;style&gt;\n&quot;)
  20. fmt.Fprint(w, &quot;p {font-size: xx-large}\n&quot;)
  21. fmt.Fprint(w, &quot;span.time {color: red}\n&quot;)
  22. fmt.Fprint(w, &quot;&lt;/style&gt;\n&quot;)
  23. fmt.Fprint(w, &quot;&lt;/head&gt;\n&quot;)
  24. fmt.Fprint(w, &quot;&lt;body&gt;\n&quot;)
  25. //fmt.Fprint(w, &quot;The time is now &quot; + t.Format(layout))
  26. 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;)
  27. fmt.Fprint(w, &quot;&lt;/body&gt;\n&quot;)
  28. fmt.Fprint(w, &quot;&lt;/html&gt;\n&quot;)
  29. }
  30. func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
  31. fmt.Fprint(w, &quot;custom 404&quot;)
  32. }
  33. func main() {
  34. version := flag.Bool(&quot;V&quot;, false, &quot;prints current version&quot;)
  35. port := flag.String(&quot;port&quot;, &quot;8080&quot;, &quot;sets service port number&quot;)
  36. flag.Parse()
  37. if *version {
  38. fmt.Println(AppVersion)
  39. os.Exit(0)
  40. }
  41. http.HandleFunc(&quot;/time/&quot;, timeserver)
  42. http.ListenAndServe(&quot;localhost:&quot; + *port, nil)
  43. }

答案1

得分: 7

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

  1. http.HandleFunc("/", NotFoundHandler)

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

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

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

Add this line to main:

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

The handler for "/" is the catchall handler.

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

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

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:

确定