Common

module
v0.0.0-...-0fdeed9 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2019 License: MIT

README

Nameless Common Codebase

For many of our services, we rely on database connectivity, effective error handling, and easy to use logging. Rather than re-implementing this across much of our codebase, we decided to open source the implementation in hopes that it can encourage other developers to write more effective software in Go, quicker.

This repository contains a number of libraries, all self contained, which are used to organize and setup Nameless services. Ideally, the implementation is generic enough to allow any developer to integrate these tools into his/her repertoire.

To install and use any of these packages, simply do go get github.com/NamelessGroup/<package name>, and follow the corresponding instructions below regarding how to use the package.

Database Package

The database package is used to create and maintain a connection pool to a PostgreSQL database. As of right now, only PostgreSQL databases are supported. It uses Upper extensively to provide a productive access layer to your database connection.

To initialize the database, call database.Init(username, password, host, database). This will return an error object if it fails. If successful, you will be able to access the database connection with database.DB. Once you are finished, you can just call database.CloseConnection(). It is reccommended to use a defer statement in your main function which closes the database connection on scope exit.

Enviroment Package

The enviroment package is used to configure and setup the enviromental conditions of your software. At Nameless, we run software in one of two enviroments: development and production. It is important for us to distingush between the two, because these two enviroments are kept completely seperate. In our case, we check whether or not we are running on a Google Compute Engine instance to tell if we are in production or not.

To configure the enviroment, you must do a few things:

  • Set the enviromental Loader for production enviroments using enviroment.SetProductionLoader(loader).
  • Set the enviromental Loader for development enviroments using enviroment.SetDevelopmentLoader(loader).
  • (Optional) Setup the array of required enviroment variables using enviroment.SetRequiredVariables([]string).
  • Call the enviroment.Init(func() bool) error with a function to determine the current enviroment state.

If the enviroment.Init call returns an error, you should shutdown your program since it is likely that an enviroment variable is missing which could cause drastic damage to your system in certain cases, potentially rendering it useless. However, if no error is returned, you can now use methods like enviroment.IsProduction() and enviroment.Get(key)to configure your application according to its enviroment.

Errors Package

We at Nameless love native Go error handling; we think it is very clear and useful throughout the process of software development.That being said, Go error handling can be a little bit cumbersome at times due to the repetitive nature of if err != nil { return err } having to be written constantly. Our errors package attempts to stay true to Go idiomatic error handling, while adding a few extra tools such as Stackdriver Reporting, and automatic logging for potentially dangerous errors that arise during program execution.

This package is dead simple to use. To setup the package, simply set the minimum error level by calling errors.SetMinimumSeverity(severity). By default, the minimum error severity is set to errors.SeverityWarning, and this can be changed. Any generated error with greater than or equal severity levels to the minimum severity will be logged. To create a new error, just do errors.New(severity, message, arguments). The message can be a formatted string with passed arguments. This error package also supports passthrough errors from external sources which may not use the errors.NGError type. For this, simply call errors.NewFromError(severity, error).

RPCHealth Package

Nameless extensively uses gRPC to facilitate internal requests. Every one of our servers use GRPC standard health checking to ensure that our Kubernetes cluster can handle misbehaving servers. This package simply implements the health checking service for which we use throughout our codebase.

To use this package, simply pass a pointer to your grpc.Server object to the rpchealth.RegisterHealthCheckerServer(server) function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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