golang-helpers

command module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

README

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.

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 root = &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

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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