-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
219 lines (188 loc) · 6.18 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package api
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"path"
"strings"
"sync"
"time"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
Address = "localhost:56789"
)
const (
healthCheckPath = "/health"
defaultTickInterval = time.Millisecond * 5
)
var (
Certificate = []byte(`-----BEGIN CERTIFICATE-----
MIIC/jCCAeagAwIBAgIJAPGpe7jZ+B9JMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV
BAMMCWxvY2FsaG9zdDAeFw0xOTAxMDYwMzM5MDZaFw0yOTAxMDMwMzM5MDZaMBQx
EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
ggEBAMdmso6OeDGKvUvdW7n3pkht2G/QlsIf3VUcVGpM6gItoTgHmj/8I9LXAiKf
o1KVfQ7BfokI+DbKlUHfQ5Pa8thHIdOFV8XGRfJFfHxoZMGBV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ThnnaARvXmw02Fx6GxGm6DhHIzM0zxsBaki7iLGJ6R1h3sbOe
FtxrzXsCgYEA0S0lYtmv4QOQq3mLf2lfroDCirqR6f5iXsyd3KhnkH/mnm56Bv4R
gTtaIFtkFS/s+lwNYSfQC/3uVPAbGcrZKRtmh9EaJk+jnjt72TRjldBnpxkXxGlz
svOxtwq93kqwiXrvckgvX+F/8D0fUpL0TyI5NY2qhPIT0tHW/yzSWiw=
-----END RSA PRIVATE KEY-----`)
)
type Server struct {
lock *sync.Mutex
server *http.Server
tlsConfig *tls.Config
}
func NewServer() *Server {
grpcServer := grpc.NewServer()
helloworld.RegisterGreeterServer(grpcServer, greeterServer{})
httpServer := &http.Server{
Handler: h2c.NewHandler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.Header.Get("Content-Type"), "application/grpc") {
if req.ProtoMajor == 2 {
grpcServer.ServeHTTP(res, req)
} else {
res.WriteHeader(http.StatusUpgradeRequired)
}
} else if req.URL.Path == healthCheckPath {
res.WriteHeader(http.StatusOK)
} else {
res.WriteHeader(http.StatusNotFound)
}
}), &http2.Server{}),
}
return &Server{
lock: &sync.Mutex{},
server: httpServer,
}
}
func (s *Server) Serve() error {
s.lock.Lock()
l, err := net.Listen("tcp", Address)
if err != nil {
return err
}
s.lock.Unlock()
return s.server.Serve(l)
}
func (s *Server) ServeTLS() error {
s.lock.Lock()
cert, err := tls.X509KeyPair(Certificate, Key)
if err != nil {
return err
}
s.tlsConfig = &tls.Config{
// Specify protocols explicitly.
// This is done by `http.Server#ServeTLS` but not by other mechanisms,
// such as providing a TLS listener to `http.Server#Serve`.
// Without `h2`, the server won't handle the ALPN negotation and so
// requests that *start* with HTTP/1.1 won't get upgraded.
NextProtos: []string{"h2", "http/1.1"},
Certificates: []tls.Certificate{cert},
}
l, err := tls.Listen("tcp", Address, s.tlsConfig)
if err != nil {
return err
}
s.lock.Unlock()
return s.server.Serve(l)
}
func (s *Server) Shutdown(ctx context.Context) error {
return s.server.Shutdown(ctx)
}
func (s *Server) TLSConfig() *tls.Config {
return s.tlsConfig
}
func (s *Server) BlockUntilRunning(timeout time.Duration) error {
interval := defaultTickInterval
if timeout < interval {
interval = timeout
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
done := make(chan bool)
timer := time.AfterFunc(timeout, func() {
done <- true
})
for {
select {
case <-done:
return errors.New("timed out before server passed health check")
case <-ticker.C:
protocol := "http"
client := http.DefaultClient
if s.tlsConfig != nil {
protocol = "https"
certPool := x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(Certificate); !ok {
return errors.New("failed to create root CA pool")
}
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
},
}
}
u := fmt.Sprintf("%s://%s", protocol, path.Join(Address, healthCheckPath))
res, err := client.Get(u)
if err != nil {
return err
}
if res.StatusCode == http.StatusOK {
timer.Stop()
return nil
}
}
}
}
type greeterServer struct{}
func (s greeterServer) SayHello(ctx context.Context, req *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
return &helloworld.HelloReply{
Message: fmt.Sprintf("Hello %s", req.Name),
}, nil
}