-
Notifications
You must be signed in to change notification settings - Fork 78
/
api_response.go
130 lines (114 loc) · 2.72 KB
/
api_response.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
package common
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"errors"
"github.com/julienschmidt/httprouter"
)
var (
ErrDeprecatedAPI = errors.New("deprecated api")
)
type Decorator func(APIHandler) APIHandler
type APIHandler func(http.ResponseWriter, *http.Request, httprouter.Params) (interface{}, error)
type HttpErr struct {
Code int
Text string
}
func (e HttpErr) Error() string {
return e.Text
}
func PlainText(f APIHandler) APIHandler {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
code := 200
data, err := f(w, req, ps)
if err != nil {
code = err.(HttpErr).Code
data = err.Error()
}
switch d := data.(type) {
case string:
w.WriteHeader(code)
io.WriteString(w, d)
case []byte:
w.WriteHeader(code)
w.Write(d)
default:
panic(fmt.Sprintf("unknown response type %T", data))
}
return nil, nil
}
}
func V1(f APIHandler) APIHandler {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
data, err := f(w, req, ps)
if err != nil {
RespondV1(w, err.(HttpErr).Code, err)
return nil, nil
}
RespondV1(w, 200, data)
return nil, nil
}
}
func RespondV1(w http.ResponseWriter, code int, data interface{}) {
var response []byte
var err error
var isJSON bool
if code == 200 {
switch data.(type) {
case string:
response = []byte(data.(string))
case []byte:
response = data.([]byte)
case nil:
response = []byte{}
default:
isJSON = true
response, err = json.Marshal(data)
if err != nil {
code = 500
data = err
}
}
}
if code != 200 {
isJSON = true
response = []byte(fmt.Sprintf(`{"message":"%s"}`, data))
}
if isJSON {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
}
w.WriteHeader(code)
w.Write(response)
}
func Decorate(f APIHandler, ds ...Decorator) httprouter.Handle {
decorated := f
for _, decorate := range ds {
decorated = decorate(decorated)
}
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
decorated(w, req, ps)
}
}
func HttpLog(log *LevelLogger, level int32) Decorator {
return func(f APIHandler) APIHandler {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
start := time.Now()
response, err := f(w, req, ps)
elapsed := time.Since(start)
status := 200
if e, ok := err.(HttpErr); ok {
status = e.Code
}
if (status != 304 && status != 200) || (status == 200 && log.Level() >= level) {
if log != nil && log.Logger != nil {
log.Logger.Output(2, fmt.Sprintf("%d %s %s (%s) %s",
status, req.Method, req.URL.RequestURI(), req.RemoteAddr, elapsed))
}
}
return response, err
}
}
}