-
Notifications
You must be signed in to change notification settings - Fork 12
/
gateway.go
224 lines (201 loc) · 8.19 KB
/
gateway.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
220
221
222
223
224
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package grpc
import (
"context"
"crypto/tls"
"net"
"net/http"
"sync"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
grpc_ "github.com/searKing/golang/third_party/google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
//go:generate go-option -type=Gateway
type Gateway struct {
// options
opt gatewayOption
http.Server
httpMuxToGrpc *runtime.ServeMux
Handler http.Handler
// runtime
grpcServer *grpc.Server
once sync.Once
}
func NewGateway(addr string, opts ...GatewayOption) *Gateway {
return NewGatewayTLS(addr, nil, opts...)
}
// TLSConfig optionally provides a TLS configuration for use
// by ServeTLS and ListenAndServeTLS. Note that this value is
// cloned by ServeTLS and ListenAndServeTLS, so it's not
// possible to modify the configuration with methods like
// tls.Config.SetSessionTicketKeys. To use
// SetSessionTicketKeys, use Server.Serve with a TLS Listener
// instead.
func NewGatewayTLS(addr string, tlsConfig *tls.Config, opts ...GatewayOption) *Gateway {
server := &Gateway{
Server: http.Server{
Addr: addr,
TLSConfig: tlsConfig,
},
}
return server.ApplyOptions(opts...)
}
// ListenAndServe listens on the TCP network address addr and then calls
// Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// ListenAndServe always returns a non-nil error.
func ListenAndServe(addr string, handler http.Handler, opts ...GatewayOption) error {
server := &Gateway{
Server: http.Server{
Addr: addr,
},
Handler: handler,
}
return server.ApplyOptions(opts...).ListenAndServe()
}
// ListenAndServeTLS acts identically to ListenAndServe, except that it
// expects HTTPS connections. Additionally, files containing a certificate and
// matching private key for the server must be provided. If the certificate
// is signed by a certificate authority, the certFile should be the concatenation
// of the server's certificate, any intermediates, and the CA's certificate.
func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler, opts ...GatewayOption) error {
server := &Gateway{
Server: http.Server{
Addr: addr,
},
Handler: handler,
}
return server.ApplyOptions(opts...).ListenAndServeTLS(certFile, keyFile)
}
// Serve accepts incoming HTTP connections on the listener l,
// creating a new service goroutine for each. The service goroutines
// read requests and then call handler to reply to them.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// HTTP/2 support is only enabled if the Listener returns *tls.Conn
// connections and they were configured with "h2" in the TLS
// Config.NextProtos.
//
// Serve always returns a non-nil error.
func Serve(l net.Listener, handler http.Handler, opts ...GatewayOption) error {
srv := &Gateway{Handler: handler}
return srv.ApplyOptions(opts...).Serve(l)
}
// ServeTLS accepts incoming HTTPS connections on the listener l,
// creating a new service goroutine for each. The service goroutines
// read requests and then call handler to reply to them.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// Additionally, files containing a certificate and matching private key
// for the server must be provided. If the certificate is signed by a
// certificate authority, the certFile should be the concatenation
// of the server's certificate, any intermediates, and the CA's certificate.
//
// ServeTLS always returns a non-nil error.
func ServeTLS(l net.Listener, handler http.Handler, certFile, keyFile string, opts ...GatewayOption) error {
srv := &Gateway{Handler: handler}
return srv.ApplyOptions(opts...).ServeTLS(l, certFile, keyFile)
}
func (gateway *Gateway) lazy_init(opts ...GatewayOption) {
gateway.once.Do(func() {
gateway.ApplyOptions(opts...)
tlsConfig := gateway.TLSConfig
if tlsConfig != nil {
// for grpc server
gateway.opt.grpcServerOpts.opts = append(gateway.opt.grpcServerOpts.opts, grpc.Creds(credentials.NewTLS(tlsConfig)))
// for grpc client to server
gateway.opt.grpcClientDialOpts = append(gateway.opt.grpcClientDialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
// disables transport security
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
gateway.opt.grpcClientDialOpts = append(opts, gateway.opt.grpcClientDialOpts...)
}
gateway.grpcServer = grpc.NewServer(gateway.opt.ServerOptions()...)
gateway.httpMuxToGrpc = runtime.NewServeMux(gateway.opt.srvMuxOpts...)
gateway.Server.Handler = grpc_.GrpcOrDefaultHandler(gateway.grpcServer, &serverHandler{
gateway: gateway,
})
})
}
func (gateway *Gateway) Serve(l net.Listener) error {
gateway.lazy_init()
return gateway.Server.Serve(l)
}
// ServeTLS accepts incoming connections on the Listener l, creating a
// new service goroutine for each. The service goroutines perform TLS
// setup and then read requests, calling srv.Handler to reply to them.
//
// Files containing a certificate and matching private key for the
// server must be provided if neither the Server's
// TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.
// If the certificate is signed by a certificate authority, the
// certFile should be the concatenation of the server's certificate,
// any intermediates, and the CA's certificate.
//
// ServeTLS always returns a non-nil error. After Shutdown or Close, the
// returned error is ErrServerClosed.
func (gateway *Gateway) ServeTLS(l net.Listener, certFile, keyFile string) error {
gateway.lazy_init()
return gateway.Server.ServeTLS(l, certFile, keyFile)
}
// ListenAndServe listens on the TCP network address srv.Addr and then
// calls Serve to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
//
// If srv.Addr is blank, ":http" is used.
//
// ListenAndServe always returns a non-nil error. After Shutdown or Close,
// the returned error is ErrServerClosed.
func (gateway *Gateway) ListenAndServe() error {
gateway.lazy_init()
return gateway.Server.ListenAndServe()
}
// ListenAndServeTLS listens on the TCP network address srv.Addr and
// then calls ServeTLS to handle requests on incoming TLS connections.
// Accepted connections are configured to enable TCP keep-alives.
//
// Filenames containing a certificate and matching private key for the
// server must be provided if neither the Server's TLSConfig.Certificates
// nor TLSConfig.GetCertificate are populated. If the certificate is
// signed by a certificate authority, the certFile should be the
// concatenation of the server's certificate, any intermediates, and
// the CA's certificate.
//
// If srv.Addr is blank, ":https" is used.
//
// ListenAndServeTLS always returns a non-nil error. After Shutdown or
// Close, the returned error is ErrServerClosed.
func (gateway *Gateway) ListenAndServeTLS(certFile, keyFile string) error {
gateway.lazy_init()
return gateway.Server.ListenAndServeTLS(certFile, keyFile)
}
// RegisterGRPCHandler registers grpc handler of the gateway
func (gateway *Gateway) RegisterGRPCHandler(handler GRPCHandler) {
gateway.lazy_init()
handler.Register(gateway.grpcServer)
}
// RegisterHTTPHandler registers http handler of the gateway
func (gateway *Gateway) RegisterHTTPHandler(ctx context.Context, handler HTTPHandler) error {
gateway.lazy_init()
//scheme://authority/endpoint
return handler.Register(ctx, gateway.httpMuxToGrpc, "passthrough:///"+gateway.Server.Addr, gateway.opt.grpcClientDialOpts)
}
// RegisterGRPCFunc registers grpc handler of the gateway
func (gateway *Gateway) RegisterGRPCFunc(handler func(srv *grpc.Server)) {
gateway.lazy_init()
gateway.RegisterGRPCHandler(GRPCHandlerFunc(handler))
}
// RegisterHTTPFunc registers http handler of the gateway
func (gateway *Gateway) RegisterHTTPFunc(ctx context.Context, handler func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error) error {
gateway.lazy_init()
return gateway.RegisterHTTPHandler(ctx, HTTPHandlerFunc(handler))
}