2023-12-13 17:17:37 +00:00
|
|
|
package query
|
2023-02-22 23:40:56 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-28 22:32:36 +00:00
|
|
|
"context"
|
2023-02-22 23:40:56 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2023-10-31 23:37:40 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
2023-12-13 17:17:37 +00:00
|
|
|
// newGrpcConnection parses a GRPC endpoint and creates a connection to it
|
|
|
|
func newGrpcConnection(ctx context.Context, endpoint string) (*grpc.ClientConn, error) {
|
2023-02-22 23:40:56 +00:00
|
|
|
grpcUrl, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
2023-12-13 17:17:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse grpc connection \"%s\": %v", endpoint, err)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
var creds credentials.TransportCredentials
|
2023-02-22 23:40:56 +00:00
|
|
|
switch grpcUrl.Scheme {
|
|
|
|
case "http":
|
2023-10-31 23:37:40 +00:00
|
|
|
creds = insecure.NewCredentials()
|
2023-02-22 23:40:56 +00:00
|
|
|
case "https":
|
2023-10-31 23:37:40 +00:00
|
|
|
creds = credentials.NewTLS(&tls.Config{})
|
2023-02-22 23:40:56 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown grpc url scheme: %s", grpcUrl.Scheme)
|
|
|
|
}
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
secureOpt := grpc.WithTransportCredentials(creds)
|
2023-12-13 17:17:37 +00:00
|
|
|
grpcConn, err := grpc.DialContext(ctx, grpcUrl.Host, secureOpt)
|
2023-02-22 23:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpcConn, nil
|
|
|
|
}
|