-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
53 lines (44 loc) · 1.14 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
package xgrpc
import (
"github.com/AltScore/gothic/pkg/xlogger"
"go.uber.org/zap"
"google.golang.org/grpc"
"net"
)
type Config struct {
Address string
}
type Server struct {
*grpc.Server
logger xlogger.Logger
config Config
}
func NewServer(logger xlogger.Logger, config Config, serverOptions ...grpc.ServerOption) *Server {
opts := []grpc.ServerOption{
grpc.UnaryInterceptor(NewLoggerInterceptor(logger)),
}
opts = append(opts, serverOptions...)
return &Server{
Server: grpc.NewServer(opts...),
logger: logger,
config: config,
}
}
// Start starts the grpc server
// This is a non-blocking call, it will start the server in a goroutine
func (s *Server) Start() error {
address := s.config.Address
lis, err := net.Listen("tcp", address)
if err != nil {
s.logger.Error("failed to listen for grpc server", zap.String("address", address), zap.Error(err))
return err
}
go func() {
s.logger.Info("Starting grpc server", zap.String("address", address))
if err := s.Server.Serve(lis); err != nil {
s.logger.Error("failed to start grpc server", zap.String("address", address), zap.Error(err))
panic(err)
}
}()
return nil
}