-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc_server.go
69 lines (59 loc) · 1.89 KB
/
grpc_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
package server
import (
"github.com/jmoiron/sqlx"
"github.com/ozonva/ova-journey-api/internal/kafka"
"github.com/ozonva/ova-journey-api/internal/metrics"
"net"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"github.com/ozonva/ova-journey-api/internal/api"
"github.com/ozonva/ova-journey-api/internal/config"
"github.com/ozonva/ova-journey-api/internal/repo"
desc "github.com/ozonva/ova-journey-api/pkg/ova-journey-api"
)
// GrpcServer - represents simple gRPC server wrapper
type GrpcServer struct {
configuration *config.EndpointConfiguration
producer kafka.Producer
metric metrics.Metrics
db *sqlx.DB
server *grpc.Server
errChan chan<- error
chunkSize int
}
// NewGrpcServer - creates new GrpcServer with configuration endpoint
//
// and output channel to signalize about critical errors
func NewGrpcServer(configuration *config.EndpointConfiguration, producer kafka.Producer, db *sqlx.DB, metric metrics.Metrics, chunkSize int, errChan chan<- error) *GrpcServer {
return &GrpcServer{
configuration: configuration,
producer: producer,
db: db,
errChan: errChan,
chunkSize: chunkSize,
metric: metric,
}
}
// Start - start GrpcServer
func (s *GrpcServer) Start() {
endpointAddress := s.configuration.GetEndpointAddress()
listen, err := net.Listen("tcp4", endpointAddress)
if err != nil {
log.Err(err).Msg("GRPC server: failed to listen")
s.errChan <- err
}
repository := repo.NewRepo(s.db)
s.server = grpc.NewServer()
desc.RegisterJourneyApiV1Server(s.server, api.NewJourneyAPI(repository, s.producer, s.metric, s.chunkSize))
go func() {
log.Debug().Msg("GRPC server: starting")
if err := s.server.Serve(listen); err != nil {
log.Err(err).Msg("GRPC server: failed to serve")
s.errChan <- err
}
}()
}
// Stop - graceful stop GrpcServer
func (s *GrpcServer) Stop() {
s.server.GracefulStop()
}