-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
grpc_web.go
45 lines (38 loc) · 1 KB
/
grpc_web.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
package grpc
import (
"fmt"
"net/http"
"time"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"google.golang.org/grpc"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/server/types"
)
// StartGRPCWeb starts a gRPC-Web server on the given address.
func StartGRPCWeb(grpcSrv *grpc.Server, config config.Config) (*http.Server, error) {
var options []grpcweb.Option
if config.GRPCWeb.EnableUnsafeCORS {
options = append(options,
grpcweb.WithOriginFunc(func(origin string) bool {
return true
}),
)
}
wrappedServer := grpcweb.WrapServer(grpcSrv, options...)
grpcWebSrv := &http.Server{
Addr: config.GRPCWeb.Address,
Handler: wrappedServer,
}
errCh := make(chan error)
go func() {
if err := grpcWebSrv.ListenAndServe(); err != nil {
errCh <- fmt.Errorf("[grpc] failed to serve: %w", err)
}
}()
select {
case err := <-errCh:
return nil, err
case <-time.After(types.ServerStartTime): // assume server started successfully
return grpcWebSrv, nil
}
}