43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package gatewayrpc
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
// Dial returns a Gateway chat client for target. A loopback target dials
|
|
// insecure; any other target requires a token. The cleanup closes the
|
|
// connection.
|
|
func Dial(target, token string, options ...grpc.DialOption) (*Client, func(), error) {
|
|
target = strings.TrimSpace(target)
|
|
if target == "" {
|
|
return nil, nil, fmt.Errorf("gateway target is required")
|
|
}
|
|
loopback, err := TargetIsLoopback(target)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("gateway target must be host:port: %w", err)
|
|
}
|
|
if !loopback && token == "" {
|
|
return nil, nil, fmt.Errorf(
|
|
"gateway target %q is non-loopback; non-loopback exposure requires a Bearer [REDACTED] ([services.gateway].target_token / GATEWAY_TOKEN) or TLS",
|
|
target,
|
|
)
|
|
}
|
|
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
|
|
if token != "" {
|
|
opts = append(
|
|
opts,
|
|
grpc.WithUnaryInterceptor(UnaryClientTokenInterceptor(token)),
|
|
grpc.WithStreamInterceptor(StreamClientTokenInterceptor(token)),
|
|
)
|
|
}
|
|
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
|
|
}
|