-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.go
214 lines (183 loc) · 5.3 KB
/
render.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package gors
import (
"context"
"google.golang.org/protobuf/encoding/protojson"
// "errors"
"fmt"
"io"
"strings"
"github.com/gin-gonic/gin/render"
internalrender "github.com/go-leo/gors/internal/pkg/render"
"github.com/go-leo/gox/iox"
"google.golang.org/grpc/metadata"
)
func ErrorRender(
ctx context.Context,
err error,
handler func(ctx context.Context, err error) error,
wrapper func(resp any) any,
) {
if handler != nil {
err = handler(ctx, err)
if err == nil {
return
}
}
gorserr := FromError(err)
ResponseRender(ctx, gorserr.StatusCode, gorserr.Status(), "", JSONRender, wrapper)
}
func ResponseRender(
ctx context.Context,
code int,
resp any,
contentType string,
render func(ctx context.Context, code int, resp any, contentType string),
wrapper func(resp any) any,
) {
addHeaders(ctx)
addTrailersHeader(ctx)
// wrapper response
if wrapper != nil {
resp = wrapper(resp)
}
// render
render(ctx, code, resp, contentType)
addTrailers(ctx)
}
func addHeaders(ctx context.Context) {
c := FromContext(ctx)
header := Header(ctx)
for key, values := range header {
for _, value := range values {
c.Writer.Header().Add(key, value)
}
}
}
func addTrailersHeader(ctx context.Context) {
c := FromContext(ctx)
doForwardTrailers := strings.Contains(strings.ToLower(c.GetHeader("TE")), "trailers")
if !doForwardTrailers {
return
}
trailer := Trailer(ctx)
if len(trailer) <= 0 {
return
}
for k := range trailer {
c.Writer.Header().Add("Trailer", k)
}
c.Writer.Header().Set("Transfer-Encoding", "chunked")
}
func addTrailers(ctx context.Context) {
c := FromContext(ctx)
doForwardTrailers := strings.Contains(strings.ToLower(c.GetHeader("TE")), "trailers")
if !doForwardTrailers {
return
}
trailer := Trailer(ctx)
if len(trailer) <= 0 {
return
}
for key, values := range trailer {
for _, value := range values {
c.Writer.Header().Add(key, value)
}
}
}
func BytesRender(ctx context.Context, code int, resp any, contentType string) {
FromContext(ctx).Render(code, render.Data{ContentType: contentType, Data: resp.([]byte)})
}
func StringRender(ctx context.Context, code int, resp any, contentType string) {
FromContext(ctx).Render(code, render.Data{ContentType: contentType, Data: []byte(resp.(string))})
}
func TextRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).String(code, resp.(string))
}
func HTMLRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).Render(code, render.Data{ContentType: "text/html; charset=utf-8", Data: []byte(resp.(string))})
}
func RedirectRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).Redirect(code, resp.(string))
}
func ReaderRender(ctx context.Context, code int, resp any, contentType string) {
r := resp.(io.Reader)
length := int64(-1)
l, ok := iox.Len(r)
if ok {
length = int64(l)
}
FromContext(ctx).Render(code, render.Reader{ContentType: contentType, ContentLength: length, Reader: r})
}
func JSONRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).JSON(code, resp)
}
func IndentedJSONRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).IndentedJSON(code, resp)
}
func SecureJSONRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).SecureJSON(code, resp)
}
func JSONPJSONRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).JSONP(code, resp)
}
func PureJSONRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).PureJSON(code, resp)
}
func AsciiJSONRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).AsciiJSON(code, resp)
}
func ProtoJSONRender(mo protojson.MarshalOptions) func(ctx context.Context, code int, resp any, _ string) {
return func(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).Render(code, internalrender.ProtoJSON{Data: resp, MarshalOptions: mo})
}
}
func XMLRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).XML(code, resp)
}
func YAMLRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).YAML(code, resp)
}
func ProtoBufRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).ProtoBuf(code, resp)
}
func MsgPackRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).Render(code, render.MsgPack{Data: resp})
}
func TOMLRender(ctx context.Context, code int, resp any, _ string) {
FromContext(ctx).TOML(code, resp)
}
func CustomRender(ctx context.Context, code int, resp any, _ string) {
customRender, ok := resp.(Render)
if !ok {
return
}
customRender.Render(ctx)
}
func AddGRPCMetadata(
ctx context.Context,
headerMD, trailerMD metadata.MD,
headerMatcher func(key string) (string, bool),
) {
if headerMatcher == nil {
headerMatcher = defaultOutgoingHeaderMatcher
}
header := Header(ctx)
for key, values := range headerMD {
if h, ok := headerMatcher(key); ok {
for _, v := range values {
header.Add(h, v)
}
}
}
trailer := Trailer(ctx)
for key, values := range trailerMD {
key = fmt.Sprintf("%s%s", MetadataTrailerPrefix, key)
for _, v := range values {
trailer.Add(key, v)
}
}
}
func defaultOutgoingHeaderMatcher(key string) (string, bool) {
return fmt.Sprintf("%s%s", MetadataHeaderPrefix, key), true
}
var DefaultMarshalOptions = protojson.MarshalOptions{}