forked from yaitoo/xun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer_string.go
More file actions
38 lines (31 loc) · 896 Bytes
/
Copy pathviewer_string.go
File metadata and controls
38 lines (31 loc) · 896 Bytes
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
33
34
35
36
37
38
package xun
import (
"fmt"
"net/http"
)
// StringViewer is a viewer that writes the given data as string to the http.ResponseWriter.
//
// It sets the Content-Type header to "text/plain".
type StringViewer struct {
}
var StringViewerMime = &MimeType{Type: "text", SubType: "plain"}
// MimeType returns the MIME type of the string content.
//
// It returns "text/plain".
func (*StringViewer) MimeType() *MimeType {
return StringViewerMime
}
// Render renders the given data as string to the http.ResponseWriter.
//
// It sets the Content-Type header to "text/plain; charset=utf-8".
func (*StringViewer) Render(ctx *Context, data any) error { // skipcq: RVV-B0012
var err error
ctx.Response.Header().Set("Content-Type", "text/plain; charset=utf-8")
if data == nil {
return nil
}
if ctx.Request.Method != http.MethodHead {
_, err = fmt.Fprint(ctx.Response, data)
}
return err
}