Skip to content

Commit

Permalink
feat: moved back grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
boodyvo committed Aug 26, 2024
1 parent 8264574 commit 338243b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions grpc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package grpc

import (
"crypto/tls"
"fmt"
"net/url"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

// NewGrpcConnection parses a GRPC endpoint and creates a connection to it
func NewGrpcConnection(endpoint string) (*grpc.ClientConn, error) {
grpcUrl, err := url.Parse(endpoint)
if err != nil {
return nil, err
}

var secureOpt grpc.DialOption
switch grpcUrl.Scheme {
case "http":
secureOpt = grpc.WithInsecure()
case "https":
creds := credentials.NewTLS(&tls.Config{})
secureOpt = grpc.WithTransportCredentials(creds)
default:
return nil, fmt.Errorf("unknown grpc url scheme: %s", grpcUrl.Scheme)
}

grpcConn, err := grpc.Dial(grpcUrl.Host, secureOpt)
if err != nil {
return nil, err
}

return grpcConn, nil
}

0 comments on commit 338243b

Please sign in to comment.