feat: add support for grpc web

This commit is contained in:
Johan Siebens
2022-05-15 08:24:45 +02:00
parent 2d4f614592
commit c74a082660
6 changed files with 563 additions and 8 deletions
+12 -1
View File
@@ -12,10 +12,12 @@ const (
ionscaleSystemAdminKey = "IONSCALE_ADMIN_KEY"
ionscaleAddr = "IONSCALE_ADDR"
ionscaleInsecureSkipVerify = "IONSCALE_SKIP_VERIFY"
ionscaleUseGrpcWeb = "IONSCALE_GRPC_WEB"
)
type Target struct {
addr string
useGrpcWeb bool
insecureSkipVerify bool
systemAdminKey string
}
@@ -23,11 +25,13 @@ type Target struct {
func (t *Target) prepareCommand(cmd *coral.Command) {
cmd.Flags().StringVar(&t.addr, "addr", "", "Addr of the ionscale server, as a complete URL")
cmd.Flags().BoolVar(&t.insecureSkipVerify, "tls-skip-verify", false, "Disable verification of TLS certificates")
cmd.Flags().BoolVar(&t.useGrpcWeb, "grpc-web", false, "Enables gRPC-web protocol. Useful if ionscale server is behind proxy which does not support GRPC")
cmd.Flags().StringVar(&t.systemAdminKey, "admin-key", "", "If specified, the given value will be used as the key to generate a Bearer token for the call. This can also be specified via the IONSCALE_ADMIN_KEY environment variable.")
}
func (t *Target) createGRPCClient() (api.IonscaleClient, io.Closer, error) {
addr := t.getAddr()
useGrpcWeb := t.getUseGrpcWeb()
skipVerify := t.getInsecureSkipVerify()
systemAdminKey := t.getSystemAdminKey()
@@ -36,7 +40,7 @@ func (t *Target) createGRPCClient() (api.IonscaleClient, io.Closer, error) {
return nil, nil, err
}
return ionscale.NewClient(auth, addr, skipVerify)
return ionscale.NewClient(auth, addr, skipVerify, useGrpcWeb)
}
func (t *Target) getAddr() string {
@@ -53,6 +57,13 @@ func (t *Target) getInsecureSkipVerify() bool {
return config.GetBool(ionscaleInsecureSkipVerify, false)
}
func (t *Target) getUseGrpcWeb() bool {
if t.useGrpcWeb {
return true
}
return config.GetBool(ionscaleUseGrpcWeb, false)
}
func (t *Target) getSystemAdminKey() string {
if len(t.systemAdminKey) != 0 {
return t.systemAdminKey
+5
View File
@@ -2,6 +2,7 @@ package mux
import (
"crypto/tls"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/jsiebens/ionscale/internal/config"
"github.com/soheilhy/cmux"
"golang.org/x/sync/errgroup"
@@ -26,11 +27,15 @@ func Serve(grpcServer *grpc.Server, appHandler http.Handler, metricsHandler http
cmux.HTTP2MatchHeaderFieldPrefixSendSettings("content-type", "application/grpc"),
cmux.HTTP2MatchHeaderFieldPrefixSendSettings("content-type", "application/grpc+proto"),
)
grpcWebL := mux.Match(cmux.HTTP1HeaderFieldPrefix("content-type", "application/grpc-web"))
httpL := mux.Match(cmux.Any())
grpcWebHandler := grpcweb.WrapServer(grpcServer)
g := new(errgroup.Group)
g.Go(func() error { return grpcServer.Serve(grpcL) })
g.Go(func() error { return http.Serve(grpcWebL, grpcWebHandler) })
g.Go(func() error { return http.Serve(httpL, appHandler) })
g.Go(func() error { return http.Serve(metricsL, metricsHandler) })
g.Go(func() error { return mux.Serve() })