Skip to content

Instantly share code, notes, and snippets.

@kyriediculous
Last active May 10, 2019 12:34
Show Gist options
  • Save kyriediculous/9ce4c20c651ebc4bb3dbdbd2d0ea9044 to your computer and use it in GitHub Desktop.
Save kyriediculous/9ce4c20c651ebc4bb3dbdbd2d0ea9044 to your computer and use it in GitHub Desktop.
var cfgFile string
// Client and context global vars for the cmd package
// So they can be used by our subcommands
var client blogpb.BlogServiceClient
var requestCtx context.Context
var requestOpts grpc.DialOption
func init() {
// initConfig reads in config file and ENV variables
cobra.OnInitialize(initConfig)
// After Cobra root config init, initialize the client
fmt.Println("Starting Blog Service Client")
// Establish context to timeout after 10 seconds if server does not respond
requestCtx, _ = context.WithTimeout(context.Background(), 10*time.Second)
// Establish insecure grpc options (no TLS)
requestOpts = grpc.WithInsecure()
// Dial the server, returns a client connection
conn, err := grpc.Dial("localhost:50051", requestOpts)
if err != nil {
log.Fatalf("Unable to establish client connection to localhost:50051: %v", err)
}
// Instantiate the BlogServiceClient with our client connection to the server
client = blogpb.NewBlogServiceClient(conn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment