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

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

Custom 404 Error Page in Golang for Kubernetes

问题

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

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

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

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

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

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

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

&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
&lt;pre style=&quot;word-wrap: break-word; white-space: pre-wrap;&quot;&gt;
&amp;lt;h2&amp;gt;hello Astaxie!&amp;lt;/h2&amp;gt;
&lt;/pre&gt;
&lt;/body&gt;
&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输出

而:

import "text/template"
...
t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
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:

import &quot;text/template&quot;
...
t, err := template.New(&quot;foo&quot;).Parse(`{{define &quot;T&quot;}}Hello, {{.}}!{{end}}`)
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:

确定