-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviewer_text.go
32 lines (26 loc) · 952 Bytes
/
viewer_text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package xun
import (
"net/http"
)
// TextViewer is a struct that holds an TextTemplate and is used to render text content.
type TextViewer struct {
template *TextTemplate
}
// MimeType returns the MIME type for the text content rendered by the TextViewer.
func (v *TextViewer) MimeType() *MimeType {
return &v.template.mime
}
// Render writes the text content rendered by the TextViewer to the provided http.ResponseWriter.
// It sets the Content-Type header to "text/plain; charset=utf-8" and writes the rendered content to the response.
// If there is an error executing the template, it is returned.
func (v *TextViewer) Render(w http.ResponseWriter, r *http.Request, data any) error { // skipcq: RVV-B0012
buf := BufPool.Get()
defer BufPool.Put(buf)
err := v.template.Execute(buf, data)
if err != nil {
return err
}
w.Header().Set("Content-Type", v.template.mime.String()+v.template.charset)
_, err = buf.WriteTo(w)
return err
}