Skip to content

feat(Slash gRPC Client): Add gRPC support for Slash endpoints. #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,40 @@ package dgo

import (
"context"
"crypto/x509"
"fmt"
"math/rand"
"net/url"
"strings"
"sync"

"github.com/dgraph-io/dgo/v200/protos/api"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

var slashPort = "443"

// Dgraph is a transaction aware client to a set of Dgraph server instances.
type Dgraph struct {
jwtMutex sync.RWMutex
jwt api.Jwt
dc []api.DgraphClient
}
type authCreds struct {
token string
}

func (a *authCreds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{"Authorization": a.token}, nil
}

func (a *authCreds) RequireTransportSecurity() bool {
return true
}

// NewDgraphClient creates a new Dgraph (client) for interacting with Alphas.
// The client is backed by multiple connections to the same or different
Expand All @@ -49,6 +66,32 @@ func NewDgraphClient(clients ...api.DgraphClient) *Dgraph {
return dg
}

// DialSlashGraphQLEndpoint creates a new Dgraph (client) for interacting with Alphas spawned
// in Slash backend. It requires Slash GraphQL's backend url and API Query key.
func DialSlashGraphQLEndpoint(endpoint, key string) (*Dgraph, error) {

u, err := url.Parse(endpoint)
urlParts := strings.SplitN(u.Host, ".", 2)

host := urlParts[0] + ".grpc." + urlParts[1] + ":" + slashPort
pool, err := x509.SystemCertPool()
creds := credentials.NewClientTLSFromCert(pool, "")
conn, err := grpc.Dial(
host,
grpc.WithTransportCredentials(creds),
grpc.WithPerRPCCredentials(&authCreds{key}),
)

if err != nil {
return nil, err
}

dc := api.NewDgraphClient(conn)
dg := NewDgraphClient(dc)

return dg, nil
}

// Login logs in the current client using the provided credentials.
// Valid for the duration the client is alive.
func (d *Dgraph) Login(ctx context.Context, userid string, password string) error {
Expand Down