-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
147 lines (106 loc) · 3.34 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package server
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"github.com/americanas-go/log"
"google.golang.org/grpc"
"google.golang.org/grpc/channelz/service"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
)
type Plugin func(ctx context.Context) []grpc.ServerOption
type Server struct {
server *grpc.Server
serviceRegistrar grpc.ServiceRegistrar
options *Options
}
func NewServer(ctx context.Context, plugins ...Plugin) *Server {
opt, err := NewOptions()
if err != nil {
panic(err)
}
return NewServerWithOptions(ctx, opt, plugins...)
}
func NewServerWithOptions(ctx context.Context, opt *Options, plugins ...Plugin) *Server {
logger := log.FromContext(ctx)
var s *grpc.Server
var serverOptions []grpc.ServerOption
if opt.TLS.Enabled {
logger.Debug("configuring tls on grpc server")
var creds credentials.TransportCredentials
certPool := x509.NewCertPool()
if opt.TLS.CertFile != "" && opt.TLS.KeyFile != "" {
logger.Trace("configuring cert and key certificates on grpc server")
// Load the certificates from disk
certificate, err := tls.LoadX509KeyPair(opt.TLS.CertFile, opt.TLS.KeyFile)
if err != nil {
logger.Fatalf("could not load server key pair: %s", err.Error())
}
logger.Trace("cert and key certificates loaded")
if opt.TLS.CAFile != "" {
logger.Trace("configuring ca certificate on grpc server")
ca, err := ioutil.ReadFile(opt.TLS.CAFile)
if err != nil {
logger.Fatalf("could not read ca certificate: %s", err.Error())
}
// Append the client certificates from the CA
if ok := certPool.AppendCertsFromPEM(ca); !ok {
logger.Fatalf("failed to append client certs")
}
logger.Trace("ca certificate loaded")
}
// Create the TLS credentials
creds = credentials.NewTLS(&tls.Config{
ClientAuth: tls.NoClientCert,
Certificates: []tls.Certificate{certificate},
ClientCAs: certPool,
})
} else {
creds = credentials.NewTLS(&tls.Config{
ClientAuth: tls.NoClientCert,
Certificates: []tls.Certificate{},
ClientCAs: certPool,
})
}
serverOptions = append(serverOptions, grpc.Creds(creds))
}
for _, plugin := range plugins {
sopts := plugin(ctx)
if sopts != nil {
serverOptions = append(serverOptions, sopts...)
}
}
serverOptions = append(serverOptions, grpc.MaxConcurrentStreams(uint32(opt.MaxConcurrentStreams)))
serverOptions = append(serverOptions, grpc.InitialConnWindowSize(opt.InitialConnWindowSize))
serverOptions = append(serverOptions, grpc.InitialWindowSize(opt.InitialWindowSize))
s = grpc.NewServer(serverOptions...)
return &Server{
server: s,
options: opt,
}
}
func (s *Server) Server() *grpc.Server {
return s.server
}
func (s *Server) ServiceRegistrar() grpc.ServiceRegistrar {
return s.server
}
func (s *Server) Serve(ctx context.Context) {
logger := log.FromContext(ctx)
service.RegisterChannelzServiceToServer(s.server)
// Register reflection service on gRPC server.
reflection.Register(s.server)
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.options.Port))
if err != nil {
logger.Fatalf("failed to listen: %v", err.Error())
}
logger.Infof("grpc server started on port %v", s.options.Port)
logger.Error(s.server.Serve(lis))
}
func (s *Server) Shutdown(ctx context.Context) {
s.server.GracefulStop()
}