/
service_handler_main.go
98 lines (76 loc) · 2.3 KB
/
service_handler_main.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
package handler
import (
"context"
"github.com/SonicRoshan/falcon"
"github.com/jinzhu/copier"
"go.uber.org/zap"
"github.com/SonicRoshan/Velocity/global/config"
"github.com/SonicRoshan/Velocity/global/logger"
proto "github.com/SonicRoshan/Velocity/services/users-srv/proto"
"github.com/SonicRoshan/Velocity/services/users-srv/users"
)
var log = logger.GetLogger("users_service.log")
//UsersService is to used to handle user service
type UsersService struct {
users users.Users
errHandler *falcon.ErrorHandler
}
//Init is used to initialize
func (usersService *UsersService) Init() error {
log.Debug("Service Initializing")
usersService.users.Init()
usersService.errHandler = falcon.NewErrorHandler()
usersService.errHandler.AddHandler(config.DefaultErrorHandler)
return nil
}
//GetByUsernameOrEmail is used to handle GetByUsernameOrEmail function
func (usersService UsersService) GetByUsernameOrEmail(
ctx context.Context,
request *proto.GetByUsernameOrEmailRequest,
response *proto.UserMain) error {
log.Debug(
"Getting User By Username Or Email",
zap.String("Username", request.Username),
zap.String("Email", request.Email),
)
userData, msg := usersService.users.GetByUsernameOrEmail(
request.Username, request.Email)
response.Message = msg
log.Info("Got Data From Low Level",
zap.Any("UserData", userData),
zap.String("message", msg),
)
copier.Copy(&response, &userData)
log.Info(
"Got User By Username Or Email",
zap.String("Username", request.Username),
zap.String("Email", request.Email),
zap.Any("UserData", userData),
zap.Any("Response", response),
)
return nil
}
//Auth is used to handle Auth function
func (usersService UsersService) Auth(
ctx context.Context, request *proto.AuthRequest, response *proto.AuthResponse) error {
log.Debug(
"Authenticating User",
zap.String("Username", request.Username),
zap.String("Email", request.Email),
)
valid, userID, msg := usersService.users.Auth(
request.Username, request.Email, request.Password)
log.Info(
"Authenticated User",
zap.String("Username", request.Username),
zap.String("Email", request.Email),
)
response.Message = msg
response.Valid = valid
response.UserID = userID
return nil
}
//Disconnect is used to disconnect
func (usersService UsersService) Disconnect() {
usersService.Disconnect()
}