forked from evergreen-ci/gimlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling.go
50 lines (43 loc) · 1.14 KB
/
handling.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
package gimlet
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
func writeResponse(of OutputFormat, w http.ResponseWriter, code int, data interface{}) {
w.Header().Set("Content-Type", of.ContentType())
w.WriteHeader(code)
size, err := writePayload(w, data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
grip.Warningf("encountered error '%s' writing a %d (%s) response ", err.Error(), size, of)
}
}
func writePayload(w io.Writer, data interface{}) (int, error) {
switch data := data.(type) {
case []byte:
return w.Write(data)
case string:
return w.Write([]byte(data))
case error:
return w.Write([]byte(data.Error()))
case []string:
return w.Write([]byte(strings.Join(data, "\n")))
case *bytes.Buffer:
return w.Write(data.Bytes())
case fmt.Stringer:
return w.Write([]byte(data.String()))
case io.Reader:
size, err := io.Copy(w, data)
return int(size), errors.WithStack(err)
case []io.Reader:
size, err := io.Copy(w, io.MultiReader(data...))
return int(size), errors.WithStack(err)
default:
return w.Write([]byte(fmt.Sprintf("%v", data)))
}
}