Go connecting
Authentication
This example assumes that you’re running Stargate locally with the default credentials
of cassandra/cassandra
.
For more information regarding authentication please see the
Stargate authentication and authorization docs.
The following code snippet can be used to generate the table-based auth token in the client code:
grpcEndpoint := "localhost:8090"
authEndpoint := "localhost:8081"
username := "cassandra"
passwd := "cassandra"
conn, err := grpc.Dial(grpcEndpoint, grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithPerRPCCredentials(
auth.NewTableBasedTokenProviderUnsafe(
fmt.Sprintf("http://%s/v1/auth", authEndpoint), username, passwd,
),
),
)
For a secure connection, use:
grpcEndpoint := "localhost:8090"
authEndpoint := "localhost:8081"
username := "cassandra"
passwd := "cassandra"
config := &tls.Config{
InsecureSkipVerify: false,
}
conn, err := grpc.Dial(grpcEndpoint, grpc.WithTransportCredentials(credentials.NewTLS(config)),
grpc.WithBlock(),
grpc.WithPerRPCCredentials(
auth.NewTableBasedTokenProvider(
fmt.Sprintf("http://%s/v1/auth", authEndpoint), username, passwd,
),
),
)
Set up client
To connect to your Stargate instance, set up the client as follows.
stargateClient, err = client.NewStargateClientWithConn(conn)
if err != nil {
fmt.Printf("error creating client %v", err)
os.Exit(1)
}
fmt.Printf("made client\n")