forked from lwl1989/ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst.go
56 lines (44 loc) · 892 Bytes
/
const.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
package ws
import (
"encoding/json"
"net/http"
"strconv"
)
type DefaultResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
var (
newline = []byte{'\n'}
space = []byte{' '}
)
func (res DefaultResponse) Error() string {
return res.Msg
}
func (res DefaultResponse) ToJsonBytes() []byte {
b, err := json.Marshal(res)
if err != nil {
return []byte{}
}
return b
}
func (res DefaultResponse) GetData() interface{} {
return res.Data
}
func (res DefaultResponse) ToJsonString() string {
b := res.ToJsonBytes()
return string(b[:])
}
func (res DefaultResponse) GetCode() int {
code, err := strconv.Atoi(res.Code)
if err != nil {
code = 200
}
return code
}
func Response(res http.ResponseWriter, response IResponse) {
code := response.GetCode()
res.WriteHeader(code)
res.Write(response.ToJsonBytes())
}