Skip to content

Latest commit

 

History

History
90 lines (61 loc) · 2.39 KB

README.md

File metadata and controls

90 lines (61 loc) · 2.39 KB

golang-helpers

Common Golang helpers

gRPC

Because sometimes you need to be able to debug gRPC services in a standalone manner.

I love gRPC. It allows me to build apps in a way that I like - a NestJS control plane with Golang microservices that do all the hard work. This allows me to build reliable services that can be widely scaled.

One problem with gRPC is that it can be hard to invoke functions to do either end-to-end tests or actually check how something works. So I created a helper library that creates a simple Cobra app with two commands:

Root

Usage:
  go run .

This is the command to use in production. This command creates the standard gRPC server with both Reflection and Health Checks enabled by default.

Run

Usage:
  go run . run <cmd> <args>

This is the command to use in development. This sets up an individual gRPC command as a Cobra command. You can the gRPC inputs via Flags and use sensible defaults where necessary.

The response from the implementation is printed to your terminal, including any sensitive information, so this should be used for local development only.

This handles both single responses and streamed responses. A StreamResponse struct exists to mock the gRPC streaming dependency which sends any data sent to it to the terminal.

Example

Example application

Logger

This is useful for getting a custom log level in a Cobra program.

package cmd

import "github.com/mrsimonemms/golang-helpers/logger"

var logLevel string

var rootCmd = &cobra.Command{
  PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    return logger.SetLevel(logLevel)
  },
}

func init() {
  rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", logrus.InfoLevel.String(), fmt.Sprintf("log level: %s", logger.GetAllLevels()))
}

Contributing

Open in a container