Skip to content

acacio/grpcutil

Repository files navigation

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)