Documentation
¶
Overview ¶
Package grpc provides client-side gRPC bootstrap helpers for the config module.
It wraps grpc.ClientConn setup so application code can create generated proto clients with consistent transport configuration, TLS or mTLS resolution, and outbound tracing.
The package is transport-oriented: it does not depend on a concrete generated client. Any generated gRPC client can be created through BuildClient.
When Connect creates the connection itself, it also attaches:
- TLS or mTLS transport credentials resolved from viper under client.grpc.*
- unary and stream interceptors that trace outbound requests through the logger package
Supported configuration keys:
- client.grpc.tls.enable
- client.grpc.tls.caFile
- client.grpc.tls.serverName
- client.grpc.tls.version
- client.grpc.tls.insecureSkipVerify
- client.grpc.mtls.enable
- client.grpc.mtls.certFile
- client.grpc.mtls.keyFile
You can also bypass viper-based transport resolution by calling SetTLSConfig with a prebuilt *tls.Config before Connect.
Tracing stores metadata, target, method name, status code, request payload, and response payload in formatter.Service entries, mirroring the HTTP client instrumentation used elsewhere in the module.
Basic usage:
package main
import (
"context"
"log"
clientgrpc "github.com/PointerByte/forge-go/config/client/grpc"
pb "github.com/PointerByte/forge-go/config/proto"
"google.golang.org/grpc/metadata"
)
func main() {
cli := clientgrpc.NewIClient(nil)
cli.SetAddress("localhost:50051")
greeter, err := clientgrpc.BuildClient(cli, pb.NewGreeterClient)
if err != nil {
log.Fatal(err)
}
ctx := metadata.AppendToOutgoingContext(
context.Background(),
"authorization", "Bearer <JWT>",
)
resp, err := greeter.SayHello(ctx, &pb.HelloRequest{Name: "Manuel"})
if err != nil {
log.Fatal(err)
}
log.Println(resp.GetMessage())
}
The same metadata pattern applies to streams: pass the outgoing metadata context when creating the generated client stream.
Index ¶
- func BuildClient[T any](client IClient, build BuildClientFunc[T]) (T, error)
- func SetTLSConfig(config *tls.Config)
- type BuildClientFunc
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect() error
- func (c *Client) GetConn() *grpc.ClientConn
- func (c *Client) SetAddress(address string)
- func (c *Client) SetConn(conn *grpc.ClientConn)
- func (c *Client) SetContext(ctx context.Context)
- func (c *Client) SetDialOptions(opts ...grpc.DialOption)
- type IClient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildClient ¶
func BuildClient[T any](client IClient, build BuildClientFunc[T]) (T, error)
BuildClient creates any generated proto client from the current connection.
func SetTLSConfig ¶
SetTLSConfig sets the TLS configuration that Connect should use when it has to create a new grpc.ClientConn and no explicit transport credentials were provided through SetDialOptions.
Types ¶
type BuildClientFunc ¶
type BuildClientFunc[T any] func(grpc.ClientConnInterface) T
BuildClientFunc creates a generated proto client from a grpc connection. Example:
greeter, err := client_gRPC.BuildClient(cli, proto.NewGreeterClient)
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) GetConn ¶
func (c *Client) GetConn() *grpc.ClientConn
func (*Client) SetAddress ¶
func (*Client) SetConn ¶
func (c *Client) SetConn(conn *grpc.ClientConn)
func (*Client) SetContext ¶
func (*Client) SetDialOptions ¶
func (c *Client) SetDialOptions(opts ...grpc.DialOption)
type IClient ¶
type IClient interface {
SetAddress(address string)
SetConn(conn *grpc.ClientConn)
SetContext(ctx context.Context)
SetDialOptions(opts ...grpc.DialOption)
Connect() error
Close() error
GetConn() *grpc.ClientConn
}
IClient defines the transport operations required to configure and manage a gRPC client connection.
It is protocol-agnostic: any client generated in the proto package can be created from the stored connection.
func NewIClient ¶
func NewIClient(conn *grpc.ClientConn) IClient
NewIClient creates a new gRPC client wrapper.
If conn is nil, Connect will create one from the configured address. If no dial options are set, the package resolves TLS/mTLS from configuration and falls back to insecure transport credentials when TLS is disabled.