-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.go
330 lines (291 loc) · 10.2 KB
/
main.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"net/textproto"
"os"
"os/signal"
"reflect"
"syscall"
"time"
"cloud.google.com/go/profiler"
"github.com/interuss/dss/pkg/api/v1/auxpb"
"github.com/interuss/dss/pkg/api/v1/ridpb"
"github.com/interuss/dss/pkg/api/v1/scdpb"
"github.com/interuss/dss/pkg/build"
"github.com/interuss/dss/pkg/errors"
"github.com/interuss/dss/pkg/logging"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/interuss/stacktrace"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/status"
)
var (
address = flag.String("addr", ":8080", "Local address that the gateway binds to and listens on for incoming connections")
traceRequests = flag.Bool("trace-requests", false, "Logs HTTP request/response pairs to stderr if true")
coreService = flag.String("core-service", "", "Endpoint for core service. Only to be set if run in proxy mode")
profServiceName = flag.String("gcp_prof_service_name", "", "Service name for the Go profiler")
enableSCD = flag.Bool("enable_scd", false, "Enables the Strategic Conflict Detection API")
)
// RunHTTPProxy starts the HTTP proxy for the DSS gRPC service on ctx, listening
// on address, proxying to endpoint.
func RunHTTPProxy(ctx context.Context, ctxCanceler func(), address, endpoint string) error {
logger := logging.WithValuesFromContext(ctx, logging.Logger).With(
zap.String("address", address), zap.String("endpoint", endpoint),
)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
runtime.HTTPError = myHTTPError
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
grpcMux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
OrigName: true,
EmitDefaults: true, // Include empty JSON arrays.
Indent: " ",
}),
)
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
//lint:ignore SA1019 This is required as an argument to a generated function.
grpc.WithTimeout(10 * time.Second),
}
if err := ridpb.RegisterDiscoveryAndSynchronizationServiceHandlerFromEndpoint(ctx, grpcMux, endpoint, opts); err != nil {
return err
}
if err := auxpb.RegisterDSSAuxServiceHandlerFromEndpoint(ctx, grpcMux, endpoint, opts); err != nil {
return err
}
if *enableSCD {
if err := scdpb.RegisterUTMAPIUSSDSSAndUSSUSSServiceHandlerFromEndpoint(ctx, grpcMux, endpoint, opts); err != nil {
return err
}
logger.Info("config", zap.Any("scd", "enabled"))
} else {
logger.Info("config", zap.Any("scd", "disabled"))
}
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/healthy" {
if _, err := w.Write([]byte("ok")); err != nil {
logger.Error("Error writing to /healthy")
}
} else {
grpcMux.ServeHTTP(w, r)
}
})
if *traceRequests {
handler = logging.HTTPMiddleware(logger, handler)
}
logger.Info("build", zap.Any("description", build.Describe()))
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(signals)
server := &http.Server{
Addr: address,
Handler: handler,
}
go func() {
defer func() {
if err := server.Shutdown(context.Background()); err != nil {
logger.Warn("failed to shut down http server", zap.Error(err))
}
}()
for {
select {
case <-ctx.Done():
logger.Info("stopping server due to context having been canceled")
return
case s := <-signals:
logger.Info("received OS signal", zap.Stringer("signal", s))
ctxCanceler()
}
}
}()
// Start HTTP server (and proxy calls to gRPC server endpoint)
return server.ListenAndServe()
}
func myCodeToHTTPStatus(code codes.Code) int {
switch code {
case codes.OK:
return http.StatusOK
case codes.Canceled:
return http.StatusRequestTimeout
case codes.Unknown:
return http.StatusInternalServerError
case codes.InvalidArgument:
return http.StatusBadRequest
case codes.DeadlineExceeded:
return http.StatusGatewayTimeout
case codes.NotFound:
return http.StatusNotFound
case codes.AlreadyExists:
return http.StatusConflict
case codes.PermissionDenied:
return http.StatusForbidden
case codes.Unauthenticated:
return http.StatusUnauthorized
case codes.ResourceExhausted:
return http.StatusTooManyRequests
case codes.FailedPrecondition:
// Note, this deliberately doesn't translate to the similarly named '412 Precondition Failed' HTTP response status.
return http.StatusBadRequest
case codes.Aborted:
return http.StatusConflict
case codes.OutOfRange:
return http.StatusBadRequest
case codes.Unimplemented:
return http.StatusNotImplemented
case codes.Internal:
return http.StatusInternalServerError
case codes.Unavailable:
return http.StatusServiceUnavailable
case codes.DataLoss:
return http.StatusInternalServerError
case codes.Code(uint16(errors.AreaTooLarge)):
return http.StatusRequestEntityTooLarge
case codes.Code(uint16(errors.MissingOVNs)):
return http.StatusConflict
}
grpclog.Warningf("Unknown gRPC error code: %v", code)
return http.StatusInternalServerError
}
// this method was copied directly from github.com/grpc-ecosystem/grpc-gateway/runtime/errors
// we initially only needed to add 1 extra Code to handle but since they didn't
// export HTTPStatusFromCode we had to copy the whole thing. Since then, we have added
// custom error handling to return additional content for certain errors. This handler
// is invoked whenever the call to the Core Service results in an error (thus returning
// a Status err). Because an error has occurred, the normal response body is not returned.
func myHTTPError(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
errID := errors.MakeErrID()
fallback := fmt.Sprintf(
`{"error": "Internal server error (fallback) %s", "message": "Internal server error (fallback) %s", "error_id": "%s", "code": %d}`,
errID, errID, errID, codes.Internal)
s, ok := status.FromError(err)
if !ok {
s = status.New(codes.Unknown, err.Error())
}
w.Header().Del("Trailer")
contentType := marshaler.ContentType()
// Check marshaler on run time in order to keep backwards compatibility
// An interface param needs to be added to the ContentType() function on
// the Marshal interface to be able to remove this check
if httpBodyMarshaler, ok := marshaler.(*runtime.HTTPBodyMarshaler); ok {
pb := s.Proto()
contentType = httpBodyMarshaler.ContentTypeFromMessage(pb)
}
w.Header().Set("Content-Type", contentType)
// Marshal error content into buf
var buf []byte
var marshalingErr error
handled := false
if s.Code() == codes.Code(uint16(errors.MissingOVNs)) {
// Handle special return schema for missing OVNs
if len(s.Details()) < 1 {
marshalingErr = stacktrace.NewError("Missing Details from Status")
} else {
body, ok := s.Details()[0].(*scdpb.AirspaceConflictResponse)
if ok {
buf, marshalingErr = marshaler.Marshal(body)
grpclog.Errorf("Error %s was an AirspaceConflictResponse from the Core Service", errID)
} else {
marshalingErr = stacktrace.NewError("Unable to cast s.Details()[0] from %s to *scdpb.AirspaceConflictResponse", reflect.TypeOf(s.Details()[0]))
}
}
handled = true
} else if len(s.Details()) == 1 {
// Handle explicit error responses
result, ok := s.Details()[0].(*auxpb.StandardErrorResponse)
if ok {
buf, marshalingErr = marshaler.Marshal(result)
grpclog.Errorf("Error %s was a StandardErrorResponse from the Core Service", errID)
handled = true
}
}
if !handled {
// Default error-handling schema
body := &auxpb.StandardErrorResponse{
Error: s.Message(),
Message: s.Message(),
Code: int32(s.Code()),
ErrorId: errID,
}
grpclog.Errorf("Error %s during a request did not include Details in Status; constructed code %d, message `%s`", errID, body.Code, body.Message)
buf, marshalingErr = marshaler.Marshal(body)
if marshalingErr != nil {
grpclog.Errorf("Error %s: Failed to marshal default error response %q: %v", errID, body, marshalingErr)
}
} else if marshalingErr != nil {
grpclog.Errorf("Error %s: Failed to marshal response: %v", errID, marshalingErr)
}
if marshalingErr != nil {
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, fallback); err != nil {
grpclog.Errorf("Error %s: Failed to write response: %v", errID, err)
}
return
}
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
grpclog.Errorf("Error %s: Failed to extract ServerMetadata from context", errID)
}
handleForwardResponseServerMetadata(w, mux, md)
handleForwardResponseTrailerHeader(w, md)
st := myCodeToHTTPStatus(s.Code())
w.WriteHeader(st)
if _, err := w.Write(buf); err != nil {
grpclog.Errorf("Error %s: Failed to write response: %v", errID, err)
}
handleForwardResponseTrailer(w, md)
}
func handleForwardResponseServerMetadata(w http.ResponseWriter, mux *runtime.ServeMux, md runtime.ServerMetadata) {
for k, vs := range md.HeaderMD {
if h, ok := runtime.DefaultHeaderMatcher(k); ok {
for _, v := range vs {
w.Header().Add(h, v)
}
}
}
}
func handleForwardResponseTrailerHeader(w http.ResponseWriter, md runtime.ServerMetadata) {
for k := range md.TrailerMD {
tKey := textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", runtime.MetadataTrailerPrefix, k))
w.Header().Add("Trailer", tKey)
}
}
func handleForwardResponseTrailer(w http.ResponseWriter, md runtime.ServerMetadata) {
for k, vs := range md.TrailerMD {
tKey := fmt.Sprintf("%s%s", runtime.MetadataTrailerPrefix, k)
for _, v := range vs {
w.Header().Add(tKey, v)
}
}
}
func main() {
flag.Parse()
var (
ctx, cancel = context.WithCancel(context.Background())
logger = logging.WithValuesFromContext(ctx, logging.Logger)
)
defer cancel()
if *profServiceName != "" {
err := profiler.Start(
profiler.Config{
Service: *profServiceName})
if err != nil {
logger.Panic("Failed to start the profiler ", zap.Error(err))
}
}
switch err := RunHTTPProxy(ctx, cancel, *address, *coreService); err {
case nil, context.Canceled, http.ErrServerClosed:
logger.Info("Shutting down gracefully")
default:
logger.Panic("Failed to execute service", zap.Error(err))
}
}