forked from hungdv136/rio
/
server.go
75 lines (61 loc) · 1.85 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package grpc
import (
"context"
"net"
"os/signal"
"syscall"
"github.com/manabie-com/rio"
"github.com/manabie-com/rio/internal/log"
fs "github.com/manabie-com/rio/internal/storage"
"google.golang.org/grpc"
_ "google.golang.org/grpc/encoding/gzip" // blank import for gzip decompress
health "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
// Server serves grpc incoming requests
type Server struct {
listener net.Listener
grpcServer *grpc.Server
}
func NewServer(stubStore rio.StubStore, fileStorage fs.FileStorage, descriptor *ServiceDescriptor) *Server {
handler := newHandler(stubStore, fileStorage, descriptor)
grpcServer := grpc.NewServer(grpc.UnknownServiceHandler(handler.handleRequest))
health.RegisterHealthServer(grpcServer, &HealthService{})
reflection.Register(grpcServer)
return &Server{grpcServer: grpcServer}
}
// Start starts the grpc server
func (s *Server) Start(ctx context.Context, addr string) error {
if err := s.prepareServer(ctx, addr); err != nil {
return err
}
return s.serve(ctx)
}
// StartAsync starts server in a different goroutine
func (s *Server) StartAsync(ctx context.Context, addr string) error {
if err := s.prepareServer(ctx, addr); err != nil {
return err
}
go func() { _ = s.serve(ctx) }()
return nil
}
func (s *Server) prepareServer(ctx context.Context, addr string) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Error(ctx, err)
return err
}
s.listener = listener
return nil
}
func (s *Server) serve(ctx context.Context) error {
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGKILL)
defer cancel()
go func() {
<-ctx.Done()
log.Info(ctx, "shutting down server")
s.grpcServer.GracefulStop()
}()
log.Info(ctx, "starting server at address", s.listener.Addr().String())
return s.grpcServer.Serve(s.listener)
}