Skip to content

Commit 95de6af

Browse files
authored
Run gRPC web proxy as part of server process (textileio#97)
Signed-off-by: Aaron Sutula <hi@asutula.com>
1 parent f5fe38a commit 95de6af

5 files changed

Lines changed: 88 additions & 30 deletions

File tree

api/client/test_utils.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ import (
1010
)
1111

1212
var (
13-
grpcHostNetwork = "tcp"
14-
grpcHostAddress = "127.0.0.1:50051"
15-
ctx = context.Background()
13+
grpcHostNetwork = "tcp"
14+
grpcHostAddress = "127.0.0.1:5002"
15+
grpcWebProxyAddress = "127.0.0.1:6002"
16+
ctx = context.Background()
1617
)
1718

1819
func setupServer(t *testing.T) func() {
1920
lotusAddr, token := tests.ClientConfigMA()
2021
conf := server.Config{
21-
LotusAddress: lotusAddr,
22-
LotusAuthToken: token,
23-
GrpcHostNetwork: grpcHostNetwork,
24-
GrpcHostAddress: grpcHostAddress,
22+
LotusAddress: lotusAddr,
23+
LotusAuthToken: token,
24+
// ToDo: Support secure gRPC connection
25+
GrpcHostNetwork: grpcHostNetwork,
26+
GrpcHostAddress: grpcHostAddress,
27+
GrpcWebProxyAddress: grpcWebProxyAddress,
2528
}
2629
server, err := server.NewServer(conf)
2730
checkErr(t, err)

api/server/server.go

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package server
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net"
78
"net/http"
89
"os"
910
"path/filepath"
11+
"time"
1012

13+
"github.com/improbable-eng/grpc-web/go/grpcweb"
1114
"github.com/ipfs/go-datastore"
1215
badger "github.com/ipfs/go-ds-badger2"
1316
logging "github.com/ipfs/go-log"
@@ -59,18 +62,21 @@ type Server struct {
5962
minerService *miner.Service
6063
slashingService *slashing.Service
6164

62-
rpc *grpc.Server
65+
grpcServer *grpc.Server
66+
grpcWebProxy *http.Server
6367

6468
closeLotus func()
6569
}
6670

6771
// Config specifies server settings.
6872
type Config struct {
69-
LotusAddress ma.Multiaddr
70-
LotusAuthToken string
71-
GrpcHostNetwork string
72-
GrpcHostAddress string
73-
RepoPath string
73+
LotusAddress ma.Multiaddr
74+
LotusAuthToken string
75+
GrpcHostNetwork string
76+
GrpcHostAddress string
77+
GrpcServerOpts []grpc.ServerOption
78+
GrpcWebProxyAddress string
79+
RepoPath string
7480
}
7581

7682
// NewServer starts and returns a new server with the given configuration.
@@ -125,6 +131,30 @@ func NewServer(conf Config) (*Server, error) {
125131
minerService := miner.NewService(mi)
126132
slashingService := slashing.NewService(si)
127133

134+
grpcServer := grpc.NewServer(conf.GrpcServerOpts...)
135+
136+
wrappedServer := grpcweb.WrapServer(
137+
grpcServer,
138+
grpcweb.WithOriginFunc(func(origin string) bool {
139+
return true
140+
}),
141+
grpcweb.WithWebsockets(true),
142+
grpcweb.WithWebsocketOriginFunc(func(req *http.Request) bool {
143+
return true
144+
}),
145+
)
146+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
147+
if wrappedServer.IsGrpcWebRequest(r) ||
148+
wrappedServer.IsAcceptableGrpcCorsRequest(r) ||
149+
wrappedServer.IsGrpcWebSocketRequest(r) {
150+
wrappedServer.ServeHTTP(w, r)
151+
}
152+
})
153+
grpcWebProxy := &http.Server{
154+
Addr: conf.GrpcWebProxyAddress,
155+
Handler: handler,
156+
}
157+
128158
s := &Server{
129159
ds: ds,
130160

@@ -144,8 +174,8 @@ func NewServer(conf Config) (*Server, error) {
144174
minerService: minerService,
145175
slashingService: slashingService,
146176

147-
// ToDo: Support secure connection
148-
rpc: grpc.NewServer(),
177+
grpcServer: grpcServer,
178+
grpcWebProxy: grpcWebProxy,
149179

150180
closeLotus: cls,
151181
}
@@ -155,13 +185,17 @@ func NewServer(conf Config) (*Server, error) {
155185
return nil, fmt.Errorf("error when listening to grpc: %s", err)
156186
}
157187
go func() {
158-
dealsPb.RegisterAPIServer(s.rpc, s.dealsService)
159-
walletPb.RegisterAPIServer(s.rpc, s.walletService)
160-
reputationPb.RegisterAPIServer(s.rpc, s.reputationService)
161-
askPb.RegisterAPIServer(s.rpc, s.askService)
162-
minerPb.RegisterAPIServer(s.rpc, s.minerService)
163-
slashingPb.RegisterAPIServer(s.rpc, s.slashingService)
164-
s.rpc.Serve(listener)
188+
dealsPb.RegisterAPIServer(grpcServer, s.dealsService)
189+
walletPb.RegisterAPIServer(grpcServer, s.walletService)
190+
reputationPb.RegisterAPIServer(grpcServer, s.reputationService)
191+
askPb.RegisterAPIServer(grpcServer, s.askService)
192+
minerPb.RegisterAPIServer(grpcServer, s.minerService)
193+
slashingPb.RegisterAPIServer(grpcServer, s.slashingService)
194+
grpcServer.Serve(listener)
195+
}()
196+
197+
go func() {
198+
grpcWebProxy.ListenAndServe()
165199
}()
166200

167201
go func() {
@@ -203,7 +237,13 @@ func NewServer(conf Config) (*Server, error) {
203237

204238
// Close shuts down the server
205239
func (s *Server) Close() {
206-
s.rpc.GracefulStop()
240+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
241+
defer cancel()
242+
243+
if err := s.grpcWebProxy.Shutdown(ctx); err != nil {
244+
log.Errorf("error shutting down proxy: %s", err)
245+
}
246+
s.grpcServer.GracefulStop()
207247
if err := s.ai.Close(); err != nil {
208248
log.Errorf("error when closing ask index: %s", err)
209249
}

exe/server/main.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import (
77
"path/filepath"
88
"syscall"
99

10+
_ "net/http/pprof"
11+
1012
"contrib.go.opencensus.io/exporter/prometheus"
1113
logging "github.com/ipfs/go-log"
1214
"github.com/textileio/fil-tools/api/server"
1315
"github.com/textileio/fil-tools/tests"
14-
_ "net/http/pprof"
1516
)
1617

1718
var (
18-
grpcHostAddr = "127.0.0.1:5002"
19+
grpcHostAddr = "0.0.0.0:5002"
20+
grpcWebProxyAddr = "0.0.0.0:6002"
1921

2022
log = logging.Logger("main")
2123
)
@@ -38,12 +40,15 @@ func main() {
3840
log.Errorf("error getting home dir: %s", err)
3941
os.Exit(-1)
4042
}
43+
// ToDo: Support secure gRPC connection
4144
conf := server.Config{
42-
LotusAddress: lotusAddr,
43-
LotusAuthToken: token,
44-
GrpcHostNetwork: "tcp",
45-
GrpcHostAddress: grpcHostAddr,
46-
RepoPath: filepath.Join(repoPath, ".texfc"),
45+
LotusAddress: lotusAddr,
46+
LotusAuthToken: token,
47+
// ToDo: Support secure gRPC connection
48+
GrpcHostNetwork: "tcp",
49+
GrpcHostAddress: grpcHostAddr,
50+
GrpcWebProxyAddress: grpcWebProxyAddr,
51+
RepoPath: filepath.Join(repoPath, ".texfc"),
4752
}
4853
log.Info("starting server...")
4954
s, err := server.NewServer(conf)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ require (
77
github.com/AlecAivazis/survey/v2 v2.0.5
88
github.com/GeertJohan/go.rice v1.0.0
99
github.com/caarlos0/spin v1.1.0
10+
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
1011
github.com/filecoin-project/go-address v0.0.0-20200107215422-da8eea2842b5
1112
github.com/filecoin-project/go-sectorbuilder v0.0.2-0.20200123143044-d9cc96c53c55
1213
github.com/filecoin-project/lotus v0.2.7-0.20200129090654-df747c8216b2
1314
github.com/golang/protobuf v1.3.3
1415
github.com/google/go-cmp v0.4.0
1516
github.com/gorilla/websocket v1.4.1
1617
github.com/gosuri/uilive v0.0.4
18+
github.com/improbable-eng/grpc-web v0.12.0
1719
github.com/ip2location/ip2location-go v8.2.0+incompatible
1820
github.com/ipfs/go-cid v0.0.4
1921
github.com/ipfs/go-datastore v0.3.1
@@ -30,6 +32,7 @@ require (
3032
github.com/multiformats/go-multiaddr v0.2.0
3133
github.com/multiformats/go-multihash v0.0.10
3234
github.com/olekukonko/tablewriter v0.0.4
35+
github.com/rs/cors v1.7.0 // indirect
3336
github.com/spf13/cobra v0.0.5
3437
github.com/spf13/viper v1.6.2
3538
github.com/stretchr/testify v1.4.0

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2
8888
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8989
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9090
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
91+
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe7Z5G/twsBW0KEalRQXZzf8ufSh9I=
92+
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f/go.mod h1:xH/i4TFMt8koVQZ6WFms69WAsDWr2XsYL3Hkl7jkoLE=
9193
github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ=
9294
github.com/dgraph-io/badger v1.6.0-rc1/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
9395
github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Evo=
@@ -225,6 +227,8 @@ github.com/huin/goupnp v0.0.0-20180415215157-1395d1447324/go.mod h1:MZ2ZmwcBpvOo
225227
github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo=
226228
github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc=
227229
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
230+
github.com/improbable-eng/grpc-web v0.12.0 h1:GlCS+lMZzIkfouf7CNqY+qqpowdKuJLSLLcKVfM1oLc=
231+
github.com/improbable-eng/grpc-web v0.12.0/go.mod h1:6hRR09jOEG81ADP5wCQju1z71g6OL4eEvELdran/3cs=
228232
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
229233
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
230234
github.com/influxdata/influxdb1-client v0.0.0-20190809212627-fc22c7df067e/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
@@ -671,6 +675,7 @@ github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wS
671675
github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
672676
github.com/multiformats/go-varint v0.0.2 h1:6sUvyh2YHpJCb8RZ6eYzj6iJQ4+chWYmyIHxszqlPTA=
673677
github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
678+
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223 h1:F9x/1yl3T2AeKLr2AMdilSD8+f9bvMnNN8VS5iDtovc=
674679
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
675680
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
676681
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
@@ -732,6 +737,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
732737
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
733738
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
734739
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
740+
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
741+
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
735742
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
736743
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
737744
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=

0 commit comments

Comments
 (0)