英文:
Custom 404 Error Page in Golang for Kubernetes
问题
我正在尝试为我的Kubernetes集群实现一个自定义的default-http镜像。我只有两个要求:
-
任何镜像都可以,只要满足以下条件:
- 在/路径下提供一个404页面
- 在/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;">
<h2>hello Astaxie!</h2>
</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:
- It serves a 404 page at /
- It serves 200 on a /healthz endpoint
As of right now, that what I got:
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 }
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 <pre></pre>
tag.
<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>
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, <script>alert('you have been pwned')</script>!
英文:
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 "text/template"
...
t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
err = t.ExecuteTemplate(out, "T", "<script>alert('you have been pwned') /script>")
produces
Hello, <script>alert('you have been pwned')</script>!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论