-
Notifications
You must be signed in to change notification settings - Fork 2
/
responses.go
246 lines (212 loc) · 6.23 KB
/
responses.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package chimera
import (
"net/http"
"reflect"
)
var (
_ ResponseWriter = new(EmptyResponse)
_ ResponseWriter = new(NoBodyResponse[Nil])
_ ResponseWriter = new(Response)
_ http.ResponseWriter = new(Response)
_ http.ResponseWriter = new(httpResponseWriter)
_ ResponseWriter = new(LazyBodyResponse)
)
// ResponseHead contains the head of an HTTP Response
type ResponseHead struct {
StatusCode int
Headers http.Header
}
type BodyWriteFunc func(body []byte) (int, error)
type ResponseBodyWriter interface {
WriteBody(write BodyWriteFunc) error
}
type ResponseHeadWriter interface {
WriteHead(*ResponseHead) error
}
// ResponseWriter allows chimera to automatically write responses
type ResponseWriter interface {
ResponseBodyWriter
ResponseHeadWriter
OpenAPIResponsesSpec() Responses
}
// ResponseWriterPtr is just a workaround to allow chimera to accept a pointer
// to a ResponseWriter and convert to the underlying type
type ResponseWriterPtr[T any] interface {
ResponseWriter
*T
}
// EmptyResponse is an empty response, effectively a no-op
// (mostly used for DELETE requests)
type EmptyResponse struct{}
// WriteHead does nothing
func (*EmptyResponse) WriteHead(*ResponseHead) error {
return nil
}
// WriteBody does nothing
func (*EmptyResponse) WriteBody(BodyWriteFunc) error {
return nil
}
// OpenAPIResponsesSpec returns an empty Responses definition
func (*EmptyResponse) OpenAPIResponsesSpec() Responses {
return Responses{}
}
// NoBodyResponse is a response with no body, but has parameters
// (mostly used for DELETE requests)
type NoBodyResponse[Params any] struct {
Params Params
}
// WriteBody does nothing
func (r *NoBodyResponse[Params]) WriteBody(BodyWriteFunc) error {
return nil
}
// OpenAPIResponsesSpec returns the parameter definitions of this object
func (r *NoBodyResponse[Params]) OpenAPIResponsesSpec() Responses {
schema := make(Responses)
pType := reflect.TypeOf(*new(Params))
for ; pType.Kind() == reflect.Pointer; pType = pType.Elem() {
}
response := ResponseSpec{}
if pType != reflect.TypeOf(Nil{}) {
response.Headers = make(map[string]Parameter)
for _, param := range CacheResponseParamsType(pType) {
response.Headers[param.Name] = Parameter{
Schema: param.Schema,
Description: param.Description,
Deprecated: param.Deprecated,
AllowReserved: param.AllowReserved,
AllowEmptyValue: param.AllowEmptyValue,
Required: param.Required,
Explode: param.Explode,
Example: param.Example,
Examples: param.Examples,
}
}
}
schema[""] = response
return schema
}
// WriteHead writes the headers for this response
func (r *NoBodyResponse[Params]) WriteHead(head *ResponseHead) error {
h, err := MarshalParams(&r.Params)
if err != nil {
return err
}
for k, v := range h {
for _, x := range v {
head.Headers.Add(k, x)
}
}
return nil
}
// NewBinaryResponse creates a NoBodyResponse from params
func NewNoBodyResponse[Params any](params Params) *NoBodyResponse[Params] {
return &NoBodyResponse[Params]{
Params: params,
}
}
// httpResponseWriter is the interal struct that overrides the default http.ResponseWriter
type httpResponseWriter struct {
writer http.ResponseWriter
respError error
response ResponseWriter
route *route
dirty bool
}
// Header returns the response headers
func (w *httpResponseWriter) Header() http.Header {
return w.writer.Header()
}
// Write writes to the response body
func (w *httpResponseWriter) Write(b []byte) (int, error) {
w.dirty = true
return w.writer.Write(b)
}
// WriteHeader sets the status code
func (w *httpResponseWriter) WriteHeader(s int) {
w.dirty = true
w.writer.WriteHeader(s)
}
// Response is a simple response type to support creating responses on the fly
// it is mostly useful for middleware where execution needs to halt and an
// undefined response needs to be returned
type Response struct {
StatusCode int
Headers http.Header
Body []byte
}
// WriteBody writes the exact body from the struct
func (r *Response) WriteBody(write BodyWriteFunc) error {
_, err := write(r.Body)
return err
}
// OpenAPIResponsesSpec returns an empty Responses object
func (r *Response) OpenAPIResponsesSpec() Responses {
return Responses{}
}
// WriteHead returns the status code and header for this response object
func (r *Response) WriteHead(head *ResponseHead) error {
if r.StatusCode > 0 {
head.StatusCode = r.StatusCode
}
for k, v := range r.Headers {
head.Headers[k] = v
}
return nil
}
// Write stores the body in the Reponse object for use later
func (r *Response) Write(body []byte) (int, error) {
if r.Body == nil {
r.Body = body
} else {
r.Body = append(r.Body, body...)
}
return len(body), nil
}
// WriteHeader stores the status code in the Reponse object for use later
func (r *Response) WriteHeader(status int) {
r.StatusCode = status
}
// Header returns the current header for http.ResponseWriter compatibility
func (r *Response) Header() http.Header {
return r.Headers
}
// NewResponse creates a response with the body, status, and header
func NewResponse(body []byte, statusCode int, header http.Header) *Response {
return &Response{
Body: body,
StatusCode: statusCode,
Headers: header,
}
}
// LazyBodyResponse is a response that effectively wraps another ReponseWriter with predefined header/status code
type LazyBodyResponse struct {
StatusCode int
Body ResponseBodyWriter
Headers http.Header
}
// WriteBody writes the exact body from the struct
func (r *LazyBodyResponse) WriteBody(write BodyWriteFunc) error {
return r.Body.WriteBody(write)
}
// OpenAPIResponsesSpec returns an empty Responses object
func (r *LazyBodyResponse) OpenAPIResponsesSpec() Responses {
return Responses{}
}
// WriteHead returns the status code and header for this response object
func (r *LazyBodyResponse) WriteHead(head *ResponseHead) error {
if r.StatusCode > 0 {
head.StatusCode = r.StatusCode
}
for k, v := range r.Headers {
head.Headers[k] = v
}
return nil
}
// NewLazyBodyResponse creates a response with predefined headers and a lazy body
func NewLazyBodyResponse(head ResponseHead, resp ResponseBodyWriter) *LazyBodyResponse {
return &LazyBodyResponse{
Body: resp,
Headers: head.Headers,
StatusCode: head.StatusCode,
}
}