rpc.go 861 B

1234567891011121314151617181920212223242526272829303132333435
  1. package remote
  2. import (
  3. "crypto/tls"
  4. "github.com/riza-io/grpc-go/credentials/basic"
  5. "google.golang.org/grpc"
  6. "google.golang.org/grpc/credentials"
  7. "github.com/sqlc-dev/sqlc/internal/config"
  8. "github.com/sqlc-dev/sqlc/internal/rpc"
  9. )
  10. const defaultHostname = "remote.sqlc.dev"
  11. func NewClient(cloudConfig config.Cloud) (GenClient, error) {
  12. authID := cloudConfig.Organization + "/" + cloudConfig.Project
  13. opts := []grpc.DialOption{
  14. grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
  15. grpc.WithPerRPCCredentials(basic.NewPerRPCCredentials(authID, cloudConfig.AuthToken)),
  16. grpc.WithUnaryInterceptor(rpc.UnaryInterceptor),
  17. }
  18. hostname := cloudConfig.Hostname
  19. if hostname == "" {
  20. hostname = defaultHostname
  21. }
  22. conn, err := grpc.Dial(hostname+":443", opts...)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return NewGenClient(conn), nil
  27. }