This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
154 lines (130 loc) · 3.56 KB
/
handler.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/0xcregis/easynode_monitor/common"
"github.com/0xcregis/easynode_monitor/common/config"
"github.com/0xcregis/easynode_monitor/service"
"github.com/gin-gonic/gin"
"github.com/sunjiangjun/xlog"
"github.com/tidwall/gjson"
)
type Handler struct {
userClient service.SysUserInterface
chainClient service.ChainNodeInterface
cfg *config.Config
log *xlog.XLog
}
func NewHandler(cfg *config.Config, x *xlog.XLog, ctx context.Context) Handler {
u := service.NewUserSrv(x, cfg)
c := service.NewChainNodeSrv(x, cfg, ctx)
return Handler{
userClient: u,
cfg: cfg,
log: x,
chainClient: c,
}
}
func (h *Handler) Authenticate(ctx *gin.Context) bool {
return true
}
func (h *Handler) Login(ctx *gin.Context) (string, error) {
b, err := io.ReadAll(ctx.Request.Body)
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
return "", fmt.Errorf("params is error")
}
root := gjson.ParseBytes(b)
account := root.Get("account").String()
pwd := root.Get("password").String()
s1 := md5.Sum([]byte(pwd))
ss := fmt.Sprintf("%x", s1)
ss = strings.ToLower(ss)
u, err := h.userClient.GetSysUser(account)
if err != nil {
return "", fmt.Errorf("params is error")
}
if u.Password != pwd {
return "", fmt.Errorf("password:%v is wrong", pwd)
}
return account, nil
}
func (h *Handler) QuerySysUser(ctx *gin.Context) {
list, err := h.userClient.QueryAllSysUser()
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
return
}
h.Success(ctx, "", list, ctx.Request.RequestURI)
}
func (h *Handler) AddSysUser(ctx *gin.Context) {
b, err := io.ReadAll(ctx.Request.Body)
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
return
}
var u common.SysUser
err = json.Unmarshal([]byte(b), &u)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
return
}
err = h.userClient.AddSysUser(&u)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
return
}
h.Success(ctx, string(b), nil, ctx.Request.RequestURI)
}
func (h *Handler) ResetPwd(ctx *gin.Context) {
b, err := io.ReadAll(ctx.Request.Body)
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
return
}
root := gjson.ParseBytes(b)
account := root.Get("account").String()
pwd := root.Get("password").String()
newPwd := root.Get("newPassword").String()
u, err := h.userClient.GetSysUser(account)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
return
}
if u.Password != pwd {
h.Error(ctx, string(b), ctx.Request.RequestURI, "the original password is wrong")
return
}
err = h.userClient.ResetPwd(account, newPwd)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
return
}
h.Success(ctx, string(b), nil, ctx.Request.RequestURI)
}
const (
SUCCESS = 0
FAIL = 1
)
func (h *Handler) Success(c *gin.Context, req string, resp interface{}, path string) {
req = strings.Replace(req, "\t", "", -1)
req = strings.Replace(req, "\n", "", -1)
h.log.Printf("path=%v,req=%v,resp=%v\n", path, req, resp)
mp := make(map[string]interface{})
mp["code"] = SUCCESS
mp["data"] = resp
c.JSON(200, mp)
}
func (h *Handler) Error(c *gin.Context, req string, path string, err string) {
req = strings.Replace(req, "\t", "", -1)
req = strings.Replace(req, "\n", "", -1)
h.log.Errorf("path=%v,req=%v,err=%v\n", path, req, err)
mp := make(map[string]interface{})
mp["code"] = FAIL
mp["data"] = err
c.JSON(200, mp)
}