/
http_response.go
48 lines (42 loc) · 1.08 KB
/
http_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
package server
import (
"encoding/json"
"net/http"
)
type JsonResult struct {
Status string `json:"status"`
Code int `json:"errCode"`
Msg string `json:"errMsg"`
Data interface{} `json:"data"`
}
type UploadRes struct {
Filepath string `json:"filepath"`
DownloadUrl string `json:"downloadUrl"`
}
type FileInfo struct {
Filepath string `json:"filepath"`
Size int64 `json:"size"`
}
func writeFailRes(w http.ResponseWriter, code int, msg string) {
data, err := json.Marshal(JsonResult{Status: "fail", Code: code, Msg: msg})
if err != nil {
log.Errorf("marshal fail res err:%s", err.Error())
return
}
_, err = w.Write(data)
if err != nil {
log.Errorf("write fail res to peer error, err:%s", err.Error())
}
}
func writeSuccessRes(w http.ResponseWriter, data interface{}) {
bytes, err := json.Marshal(JsonResult{Status: "success", Data: data})
if err != nil {
log.Errorf("marshal fail res err:%s", err.Error())
return
}
_, err = w.Write(bytes)
if err != nil {
log.Errorf("write success res to peer error, err:%s", err.Error())
return
}
}