Files
archie/internal/infrastructure/gatewayrpc/dial.go
T

37 lines
1.1 KiB
Go

package gatewayrpc
import (
"fmt"
"strings"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
)
// Dial returns a Gateway chat client for target, authenticated by id. The
// cleanup closes the connection.
func Dial(target string, id *rpctls.Identity, options ...grpc.DialOption) (*Client, func(), error) {
target = strings.TrimSpace(target)
if target == "" {
return nil, nil, fmt.Errorf("gateway target is required")
}
creds, err := id.DialOption(target, presence.Gateway)
if err != nil {
return nil, nil, fmt.Errorf("gateway: %w", err)
}
opts := []grpc.DialOption{
creds,
telemetry.ClientOption(),
grpc.WithUnaryInterceptor(unaryPrincipal),
grpc.WithStreamInterceptor(streamPrincipal),
}
conn, err := grpc.NewClient(target, append(opts, options...)...)
if err != nil {
return nil, nil, fmt.Errorf("create gateway client: %w", err)
}
return NewClient(conn), func() { _ = conn.Close() }, nil
}