grpcutil

package module
v0.0.0-...-a09be83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

README

grpcutil

gRPC Utility functions

Example use
import (
  "github.com/acacio/grpcutil"
  pb "path-to-grpc-proto/proto"
)

type MyServer struct {
  pb.Unimplemented<SOMETHING>Server
}

func NewMyServer(port, grpcwebport string) *MyServer {

  logger := grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, os.Stderr)
  grpclog.SetLoggerV2(logger)

  // Create gRPC server
  srvOpts := grpcutil.ServerOptions()
  s := grpc.NewServer(srvOpts...)

  // OPTIONAL - Registering gRPC reflection for dynamic RPC tools 
  reflection.Register(s)

  // Instantiate service
  mysrv := &MyServer{}
  pb.RegisterMyServer(s, mysrv)

  // OPTIONAL - After your registrations, Prometheus metrics are initialized.
  log.Println("Registering Prometheus...")
  grpc_prometheus.Register(s)

  // Start listening & serving requests
  grpcutil.Serve(s, port) // Fatal failure on error

  return mysrv
}
gRPCWeb

To enable GRPCWeb, before Serve(), you can add the protocol adapter and server in a goroutine:

  // OPTIONAL - Enable gRPCWeb serving. Requires goroutine
  go grpcutil.StartgRPCWeb(s, grpcwebport)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckRPCAuth

func CheckRPCAuth(ctx context.Context) error

CheckRPCAuth - used by servers to check RPC auth

func DefaultServerOptions

func DefaultServerOptions(srvOpts []grpc.ServerOption) []grpc.ServerOption

ServerOptions return the chain of ServerOptions incl. middleware

func EnablePrometheus

func EnablePrometheus(s *grpc.Server, PORT string) http.Handler

EnablePrometheus - sets up gRPC metrics collection and launch web collection endpoint

func GetgRPCHistograms

func GetgRPCHistograms() map[string]map[float64]uint64

GetgRPCHistograms grabs the data for all methods

func GetgRPCMetrics

func GetgRPCMetrics() map[string]float64

GetgRPCMetrics returns the grpc_prometheus metrics object

func KeepAliveDefault

func KeepAliveDefault() grpc.ServerOption

func PeerAddress

func PeerAddress(ctx context.Context) net.Addr

PeerAddress returns connecion peer address

func Serve

func Serve(s *grpc.Server, port string)

func SetupConnection

func SetupConnection(addr string, opts *ClientOpts) (*grpc.ClientConn, error)

SetupConnection handles base gRPC connection establishment

func StartgRPCWeb

func StartgRPCWeb(s *grpc.Server, grpcwebport string)

StartgRPCWeb encapsulates a gRPC server and serves gRPC on a new port

func TokenAuthCheck

func TokenAuthCheck(ctx context.Context, srvToken string) (context.Context, error)

TokenAuthCheck ...

func WithPerRPCToken

func WithPerRPCToken(token string) grpc.DialOption

WithPerRPCToken - helper function

Types

type BasicAuthCreds

type BasicAuthCreds struct {
	// contains filtered or unexported fields
}

BasicAuthCreds is an implementation of credentials.PerRPCCredentials that transforms the username and password into a base64 encoded value similar to HTTP Basic xxx

func (*BasicAuthCreds) Digest

func (b *BasicAuthCreds) Digest() string

helper function

func (*BasicAuthCreds) GetRequestMetadata

func (b *BasicAuthCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error)

GetRequestMetadata sets the value for "authorization" key

func (*BasicAuthCreds) RequireTransportSecurity

func (b *BasicAuthCreds) RequireTransportSecurity() bool

RequireTransportSecurity should be true as even though the credentials are base64, we want to have it encrypted over the wire.

type ClientOpts

type ClientOpts struct {
	TLSType string
	CA      string
	Cert    string
	Key     string
	Token   string
	Block   bool
}

ClientOpts configure gRPC client connection

type TokenAuth

type TokenAuth struct {
	Token string
}

TokenAuth implement gRPC PerRPCCredentials interface

func (TokenAuth) GetRequestMetadata

func (t TokenAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error)

GetRequestMetadata implements PerRPCCredentials interface Return value is mapped to request headers.

func (TokenAuth) RequireTransportSecurity

func (t TokenAuth) RequireTransportSecurity() bool

RequireTransportSecurity implements PerRPCCredentials interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL