-
Notifications
You must be signed in to change notification settings - Fork 488
/
writer.go
50 lines (40 loc) · 1.16 KB
/
writer.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 api
import (
"net/http"
"build-booster/common/blog"
http2 "build-booster/common/http"
commonTypes "build-booster/common/types"
"github.com/emicklei/go-restful"
)
// RestResponse contains all response information need by a http handler
type RestResponse struct {
Resp *restful.Response
HTTPCode int
Data interface{}
ErrCode commonTypes.ServerErrCode
Message string
Extra map[string]interface{}
WrapFunc func([]byte) []byte
}
// ReturnRest do the return work according to a RestResponse
func ReturnRest(resp *RestResponse) {
if resp.HTTPCode == 0 {
resp.HTTPCode = http.StatusOK
}
if resp.Message == "" {
resp.Message = resp.ErrCode.String()
} else {
resp.Message = resp.ErrCode.String() + " | " + resp.Message
}
result, err := http2.GetResponseEx(int(resp.ErrCode), resp.Message, resp.Data, resp.Extra)
if err != nil {
blog.Errorf("%s | err: %v", commonTypes.ServerErrEncodeJSONFailed, err)
ReturnRest(&RestResponse{Resp: resp.Resp, ErrCode: commonTypes.ServerErrEncodeJSONFailed})
return
}
if resp.WrapFunc != nil {
result = resp.WrapFunc(result)
}
resp.Resp.WriteHeader(resp.HTTPCode)
_, _ = resp.Resp.Write(result)
}