-
Notifications
You must be signed in to change notification settings - Fork 7
/
respond.go
49 lines (44 loc) · 772 Bytes
/
respond.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
package vox
import (
"encoding/json"
"io"
)
func respond(ctx *Context, req *Request, res *Response) {
ctx.Next()
if res.DontRespond {
return
}
res.setImplicit()
res.Writer.WriteHeader(res.Status)
switch v := res.Body.(type) {
case []byte:
res.Writer.Write(v)
case string:
res.Writer.Write([]byte(v))
case io.ReadCloser:
_, err := io.Copy(res.Writer, v)
if err != nil {
panic(err)
} else {
if err = v.Close(); err != nil {
panic(err)
}
}
case io.Reader:
_, err := io.Copy(res.Writer, v)
if err != nil {
panic(err)
}
case error:
_, err := res.Writer.Write([]byte(v.Error()))
if err != nil {
panic(err)
}
default:
body, err := json.Marshal(v)
if err != nil {
panic(err)
}
res.Writer.Write(body)
}
}