-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.go
190 lines (166 loc) · 5.87 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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package rest
import (
"context"
"net"
"net/http"
"strconv"
"strings"
"code.vegaprotocol.io/vega/datanode/gateway"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/paths"
protoapiv2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
vegaprotoapi "code.vegaprotocol.io/vega/protos/vega/api/v1"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
"go.elastic.co/apm/module/apmhttp"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
const (
namedLogger = "restproxy"
)
// ProxyServer implement a rest server acting as a proxy to the grpc api.
type ProxyServer struct {
gateway.Config
log *logging.Logger
vegaPaths paths.Paths
srv *http.Server
}
// NewProxyServer returns a new instance of the rest proxy server.
func NewProxyServer(log *logging.Logger, config gateway.Config, vegaPaths paths.Paths) *ProxyServer {
// setup logger
log = log.Named(namedLogger)
log.SetLevel(config.Level.Get())
return &ProxyServer{
log: log,
Config: config,
srv: nil,
vegaPaths: vegaPaths,
}
}
// ReloadConf update the internal configuration of the server.
func (s *ProxyServer) ReloadConf(cfg gateway.Config) {
s.log.Info("reloading configuration")
if s.log.GetLevel() != cfg.Level.Get() {
s.log.Info("updating log level",
logging.String("old", s.log.GetLevel().String()),
logging.String("new", cfg.Level.String()),
)
s.log.SetLevel(cfg.Level.Get())
}
// TODO(): not updating the the actual server for now, may need to look at this later
// e.g restart the http server on another port or whatever
s.Config = cfg
}
// This is because by default the marshaller wants to put a newline between chunks of the stream response.
type HTTPBodyDelimitedMarshaler struct {
runtime.HTTPBodyMarshaler
delimiter []byte
}
func (o *HTTPBodyDelimitedMarshaler) Delimiter() []byte {
return o.delimiter
}
// Start start the server.
func (s *ProxyServer) Start(ctx context.Context) (http.Handler, error) {
logger := s.log
grpcAddr := net.JoinHostPort(s.Node.IP, strconv.Itoa(s.Node.Port))
mux := runtime.NewServeMux(
// this is a settings specially made for websockets
runtime.WithMarshalerOption("application/json+stream", &JSONPb{
EnumsAsInts: true,
EmitDefaults: false,
OrigName: false,
}),
// prettified, just for JonRay
// append ?pretty to any query to make it... pretty
runtime.WithMarshalerOption("application/json+pretty", &JSONPb{
EnumsAsInts: false,
EmitDefaults: true,
OrigName: false,
Indent: " ",
}),
runtime.WithMarshalerOption("text/csv", &HTTPBodyDelimitedMarshaler{
delimiter: []byte(""), // Don't append newline between stream sends
// Default HTTPBodyMarshaler
HTTPBodyMarshaler: runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
},
}),
// default for REST request
runtime.WithMarshalerOption(runtime.MIMEWildcard, &JSONPb{
EmitDefaults: true,
OrigName: false,
}),
runtime.WithOutgoingHeaderMatcher(func(s string) (string, bool) { return s, true }),
)
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(s.Config.Node.MaxMsgSize)),
}
marshalW := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if we are dealing with a stream, let's add some header to use the proper marshaller
if strings.HasPrefix(r.URL.Path, "/api/v2/stream/") {
r.Header.Set("Accept", "application/json+stream")
} else if strings.HasPrefix(r.URL.Path, "/api/v2/networkhistory/export") ||
strings.HasPrefix(r.URL.Path, "/api/v2/ledgerentry/export") {
r.Header.Set("Accept", "text/csv")
} else if _, ok := r.URL.Query()["pretty"]; ok {
// checking Values as map[string][]string also catches ?pretty and ?pretty=
// r.URL.Query().Get("pretty") would not.
r.Header.Set("Accept", "application/json+pretty")
}
h.ServeHTTP(w, r)
})
}
if err := vegaprotoapi.RegisterCoreServiceHandlerFromEndpoint(ctx, mux, grpcAddr, opts); err != nil {
logger.Panic("Failure registering trading handler for REST proxy endpoints", logging.Error(err))
}
if err := protoapiv2.RegisterTradingDataServiceHandlerFromEndpoint(ctx, mux, grpcAddr, opts); err != nil {
logger.Panic("Failure registering trading handler for REST proxy endpoints", logging.Error(err))
}
handler := marshalW(mux)
handler = healthCheckMiddleware(handler)
handler = gateway.RemoteAddrMiddleware(logger, handler)
// Gzip encoding support
handler = NewGzipHandler(*logger, handler.(http.HandlerFunc))
// Metric support
handler = gateway.MetricCollectionMiddleware(handler)
handler = wsproxy.WebsocketProxy(handler)
// APM
if s.REST.APMEnabled {
handler = apmhttp.Wrap(handler)
}
return handler, nil
}
func healthCheckMiddleware(f http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/health" {
_, _ = w.Write([]byte("ok"))
w.WriteHeader(http.StatusOK)
return
}
f.ServeHTTP(w, r)
}
}