在Kubernetes中为Golang创建自定义404错误页面

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

Custom 404 Error Page in Golang for Kubernetes

问题

我正在尝试为我的Kubernetes集群实现一个自定义的default-http镜像。我只有两个要求:

  • 任何镜像都可以,只要满足以下条件:

    1. 在/路径下提供一个404页面
    2. 在/healthz路径下返回200

目前为止,这是我的代码:

  1. 1 package main
  2. 2
  3. 3 import (
  4. 4 "fmt"
  5. 5 "net/http"
  6. 6 "html/template"
  7. 7 )
  8. 8
  9. 9 func main() {
  10. 10 http.HandleFunc("/healthz", healhtzHandler)
  11. 11 http.HandleFunc("/", errorHandler)
  12. 12 http.ListenAndServe(":8000", nil)
  13. 13 }
  14. 14
  15. 15 func healhtzHandler(w http.ResponseWriter, r *http.Request) {
  16. 16 fmt.Fprint(w, "Really healthy")
  17. 17 }
  18. 18
  19. 19 type Person struct {
  20. 20 UserName string
  21. 21 }
  22. 22
  23. 23 func errorHandler(w http.ResponseWriter, r *http.Request) {
  24. 24 w.WriteHeader(404)
  25. 25 t := template.New("fieldname example")
  26. 26 t, _ = t.Parse("<h2>hello {{.UserName}}!</h2>")
  27. 27 p := Person{UserName: "Astaxie"}
  28. 28 t.Execute(w, p)
  29. 29 }

除了我的主要目标是显示一个酷炫的404错误页面之外,一切都按预期工作。因此,我需要使用Bootstrap、酷炫的标签等。但是这段代码总是在<pre></pre>标签中执行模板。

  1. <html>
  2. <head></head>
  3. <body>
  4. <pre style="word-wrap: break-word; white-space: pre-wrap;">
  5. &lt;h2&gt;hello Astaxie!&lt;/h2&gt;
  6. </pre>
  7. </body>
  8. </html>

这破坏了我想做的一切。我该如何解决这个问题?

英文:

I am trying to implement a custom default-http image for my Kubernetes Cluster. I only got 2 requirements:

  • Any image is permissable as long as:
  1. It serves a 404 page at /
  2. It serves 200 on a /healthz endpoint

As of right now, that what I got:

  1. 1 package main
  2. 2
  3. 3 import (
  4. 4 &quot;fmt&quot;
  5. 5 &quot;net/http&quot;
  6. 6 &quot;html/template&quot;
  7. 7 )
  8. 8
  9. 9 func main() {
  10. 10 http.HandleFunc(&quot;/healthz&quot;, healhtzHandler)
  11. 11 http.HandleFunc(&quot;/&quot;,errorHandler)
  12. 12 http.ListenAndServe(&quot;:8000&quot;, nil)
  13. 13 }
  14. 14
  15. 15 func healhtzHandler(w http.ResponseWriter, r *http.Request) {
  16. 16 fmt.Fprint(w, &quot;Really healthy&quot;)
  17. 17 }
  18. 18
  19. 19 type Person struct {
  20. 20 UserName string
  21. 21 }
  22. 22
  23. 23 func errorHandler(w http.ResponseWriter, r *http.Request) {
  24. 24 w.WriteHeader(404)
  25. 25 t := template.New(&quot;fieldname example&quot;)
  26. 26 t, _ = t.Parse(&quot;&lt;h2&gt;hello {{.UserName}}!&lt;/h2&gt;&quot;)
  27. 27 p := Person{UserName: &quot;Astaxie&quot;}
  28. 28 t.Execute(w, p)
  29. 29 }

It all works as expected, except that my main goal here is to display a cool 404 error page, so I will need to use Bootstrap, cool tags and etc. But this code always execute the template inside a &lt;pre&gt;&lt;/pre&gt; tag.

  1. &lt;html&gt;
  2. &lt;head&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;pre style=&quot;word-wrap: break-word; white-space: pre-wrap;&quot;&gt;
  5. &amp;lt;h2&amp;gt;hello Astaxie!&amp;lt;/h2&amp;gt;
  6. &lt;/pre&gt;
  7. &lt;/body&gt;
  8. &lt;/html&gt;

and it ruins everything I want to do. How do I solve this?

答案1

得分: 1

根据这个模板的文档,我认为你可能想使用"text/template"而不是"html/template"。

该页面上说:

html/template中的上下文自动转义会生成安全的、转义后的HTML输出

而:

  1. import "text/template"
  2. ...
  3. t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
  4. err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned')</script>")

会生成:

Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;!

英文:

Based on this documentation for template, I think you might want to use "text/template" instead of "html/template".

That page says:

> The contextual autoescaping in html/template produces safe, escaped HTML output

While:

  1. import &quot;text/template&quot;
  2. ...
  3. t, err := template.New(&quot;foo&quot;).Parse(`{{define &quot;T&quot;}}Hello, {{.}}!{{end}}`)
  4. err = t.ExecuteTemplate(out, &quot;T&quot;, &quot;&lt;script&gt;alert(&#39;you have been pwned&#39;) /script&gt;&quot;)

produces
Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;!

huangapple
  • 本文由 发表于 2016年9月17日 00:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/39536560.html
匿名

发表评论

匿名网友

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

确定