-
Notifications
You must be signed in to change notification settings - Fork 24
/
grpcgateway.go
230 lines (201 loc) · 6.18 KB
/
grpcgateway.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
225
226
227
228
229
230
// +kubebuilder:validation:Optional
package grpcgateway
import (
"context"
"crypto/tls"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/fluxninja/aperture/pkg/config"
"github.com/fluxninja/aperture/pkg/info"
"github.com/fluxninja/aperture/pkg/log"
commonhttp "github.com/fluxninja/aperture/pkg/net/http"
)
const (
defaultKey = "server.grpc_gateway"
)
// Module is an fx module that sets up grpc-http Gateway.
func Module() fx.Option {
return fx.Options(
Constructor{}.Annotate(),
)
}
// Constructor holds fields to configure grpc-http gateway.
type Constructor struct {
// Name of http server instance -- empty for main server. Gateway name will be the same as HTTP server name
Name string
// Config key
ConfigKey string
// Default gateway config
DefaultConfig GRPCGatewayConfig
}
// GRPCGatewayConfig holds configuration for grpc-http gateway
// swagger:model
type GRPCGatewayConfig struct {
// GRPC server address to connect to - By default it points to HTTP server port because FluxNinja stack runs GRPC and HTTP servers on the same port
GRPCAddr string `json:"grpc_server_address" validate:"hostname_port" default:"0.0.0.0:1"`
}
// GRPCGateway holds fields required for grpc-http gateway.
type GRPCGateway struct {
// Tracks gw context
ctx context.Context
// Invoking cancel will disconnect all gw client connections
cancel context.CancelFunc
grpcAddr string
gwmux *runtime.ServeMux
mux *mux.Router
dopts []grpc.DialOption
}
func (gw *GRPCGateway) registerGW(fn RegisterHandlerFunc) error {
return fn(gw.ctx, gw.gwmux, gw.grpcAddr, gw.dopts)
}
// Annotate returns fx options that set up grpc-http gateway.
func (constructor Constructor) Annotate() fx.Option {
if constructor.ConfigKey == "" {
constructor.ConfigKey = defaultKey
}
var group, name string
if constructor.Name == "" {
group = config.GroupTag(info.Service)
} else {
group = config.GroupTag(constructor.Name)
}
name = config.NameTag(constructor.Name)
return fx.Options(
fx.Invoke(
fx.Annotate(
constructor.setupGRPCGateway,
fx.ParamTags(group, name),
),
),
)
}
// RegisterHandlerFunc is a handler function that registers grpc-http gateway handlers.
type RegisterHandlerFunc func(context.Context, *runtime.ServeMux, string, []grpc.DialOption) error
// RegisterHandler holds fields to register grpc-http gateway handlers.
type RegisterHandler struct {
// Handler func to invoke on start
Handler RegisterHandlerFunc
// gateway instance to register with -- empty = default gateway
Name string
}
// Annotate annotates handler function in rh and returns it.
// These handlers can be registered with grpc-http gateway.
func (rh RegisterHandler) Annotate() fx.Option {
var group string
if rh.Name == "" {
group = config.GroupTag(info.Service)
} else {
group = config.GroupTag(rh.Name)
}
return fx.Provide(
fx.Annotate(
func() RegisterHandlerFunc { return rh.Handler },
fx.ResultTags(group),
),
)
}
func (constructor Constructor) setupGRPCGateway(
handlers []RegisterHandlerFunc,
httpServer *commonhttp.Server,
lifecycle fx.Lifecycle,
unmarshaller config.Unmarshaller,
// Client to health service
healthClient grpc_health_v1.HealthClient,
) error {
config := constructor.DefaultConfig
if err := unmarshaller.UnmarshalKey(constructor.ConfigKey, &config); err != nil {
log.Error().Err(err).Msg("Unable to deserialize grpc gateway configuration!")
return err
}
dopts := []grpc.DialOption{}
var tlsConfig *tls.Config
if httpServer.TLSConfig != nil {
tlsConfig = httpServer.TLSConfig.Clone()
// trust local server
tlsConfig.InsecureSkipVerify = true
transportCredentials := credentials.NewTLS(tlsConfig)
dopts = append(dopts, grpc.WithTransportCredentials(transportCredentials))
} else {
// connect in an insecure manner
dopts = append(dopts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
gw := &GRPCGateway{}
gw.gwmux = runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}),
runtime.WithForwardResponseOption(httpResponseModifier),
runtime.WithIncomingHeaderMatcher(httpHeaderMatcher),
runtime.WithHealthzEndpoint(healthClient),
)
gw.dopts = dopts
gw.mux = httpServer.Mux
// Set RootHandler for HTTP server
httpServer.RootHandler = gw.gwmux
// Actual address will be figured out on start because it may depend on HTTP listener address -- which is not known until it starts
gw.grpcAddr = config.GRPCAddr
gw.ctx, gw.cancel = context.WithCancel(context.Background())
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
if gw.grpcAddr == "0.0.0.0:1" {
gw.grpcAddr = httpServer.Listener.GetListener().Addr().String()
}
// register routes -- this starts making grpc connections to the grpc server
for _, handler := range handlers {
err := gw.registerGW(handler)
if err != nil {
log.Error().Err(err).Msg("Failed to register grpc gateway route!")
return err
}
}
return nil
},
OnStop: func(context.Context) error {
// Cancel the gw context
gw.cancel()
return nil
},
})
return nil
}
func httpResponseModifier(ctx context.Context, w http.ResponseWriter, _ proto.Message) error {
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
return nil
}
// set http status code
if vals := md.HeaderMD.Get("x-http-code"); len(vals) > 0 {
code, err := strconv.Atoi(vals[0])
if err != nil {
return err
}
// delete the headers to not expose any grpc-metadata in http response
delete(md.HeaderMD, "x-http-code")
delete(w.Header(), "Grpc-Metadata-X-Http-Code")
w.WriteHeader(code)
}
return nil
}
func httpHeaderMatcher(key string) (string, bool) {
switch key {
case "Apikey":
return key, true
default:
return runtime.DefaultHeaderMatcher(key)
}
}