This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (46 loc) · 1.58 KB
/
server.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
package gapi
import (
"net"
"os"
"github.com/ansh-devs/userdatalink/internal/database"
"github.com/ansh-devs/userdatalink/protos"
_ "github.com/joho/godotenv/autoload"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// UserService Definition of the UserService
type UserService struct {
protos.UnimplementedUserServiceServer
Repository database.UserRepository
Srvr *grpc.Server
Port string
Ln net.Listener
}
// NewUserService create an instance of User Service listens to the port and binds the listener to the UserService
func NewUserService() *UserService {
srv := UserService{}
srv.Port = ":" + os.Getenv("PORT")
srv.Ln = MustListenToAddr(srv.Port)
srv.Repository = database.NewUserRepository()
srv.ConfigureGapi()
return &srv
}
// ListenToAddr Attempts to listen to the port and return err if cannot
func MustListenToAddr(port string) net.Listener {
ln, err := net.Listen("tcp", port)
if err != nil {
logrus.Fatalf("error while listening the port %v", err)
}
logrus.WithFields(logrus.Fields{"message": "listening on port " + port}).Info("user_service")
return ln
}
func (s *UserService) ConfigureGapi() {
grpcLogger := grpc.UnaryInterceptor(GrpcLogger)
s.Srvr = grpc.NewServer(grpcLogger)
protos.RegisterUserServiceServer(s.Srvr, s)
reflection.Register(s.Srvr)
logrus.WithFields(logrus.Fields{"message": "ready to accept connections"}).Info("user_service")
}
// mustEmbedUnimplementedUserServiceServer implements protos.UserServiceServer.
func (b *UserService) mustEmbedUnimplementedUserServiceServer() {}