/
types.go
64 lines (54 loc) · 1.44 KB
/
types.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
package operator
import (
"encoding/json"
"net/http"
gethcmn "github.com/ethereum/go-ethereum/common"
"github.com/smartbch/cc-operator/sbch"
)
type OpInfo struct {
Status string `json:"status"`
CurrNodes []sbch.NodeInfo `json:"currNodes,omitempty"`
NewNodes []sbch.NodeInfo `json:"newNodes,omitempty"`
NodesChangedTime int64 `json:"nodesChangedTime,omitempty"`
Monitors []gethcmn.Address `json:"monitors,omitempty"`
}
type Resp struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
Result any `json:"result,omitempty"`
}
func NewResp(result any, err error) Resp {
if err != nil {
return NewErrResp(err.Error())
} else {
return NewOkResp(result)
}
}
func NewErrResp(err string) Resp {
return Resp{
Success: false,
Error: err,
}
}
func NewOkResp(result any) Resp {
return Resp{
Success: true,
Result: result,
}
}
func UnmarshalResp(jsonBytes []byte) (resp Resp) {
_ = json.Unmarshal(jsonBytes, &resp)
return
}
func (resp Resp) ToJSON() []byte {
bytes, _ := json.Marshal(resp)
return bytes
}
func (resp Resp) WriteTo(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "origin, content-type, accept")
bytes, _ := json.Marshal(resp)
_, _ = w.Write(bytes)
}