11package server
22
33import (
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.
6872type 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
205239func (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 }
0 commit comments