add h2c/http2 support with WithoutCmux option

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2023-09-16 16:21:51 +02:00
parent f455c9994c
commit c0c19683cf
3 changed files with 99 additions and 30 deletions

View File

@ -6,12 +6,14 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/metadata"
@ -130,6 +132,7 @@ func run(opts ...service.Option) {
close(done)
return nil
}),
service.WithoutCmux(),
service.WithGateway(RegisterGreeterHandler),
service.WithGatewayPrefix("/rest"),
service.WithGRPCWeb(true),
@ -242,14 +245,26 @@ func run(opts ...service.Option) {
log.Infof("received stream message: %s", m.Message)
}
scheme := "http://"
var (
tlsConfig *tls.Config
dial func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error)
)
if secure {
scheme = "https://"
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
} else {
dial = func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, network, addr)
}
}
httpc := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Transport: &http2.Transport{
AllowHTTP: true,
TLSClientConfig: tlsConfig,
DialTLSContext: dial,
},
}
req := `{"name":"test"}`