Common Golang helpers
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:
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.
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.
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()))
}