gocore

package module
v1.0.63 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 28 Imported by: 2

README

GoCore

GoCore is a library of useful tools that can be used to configure microservice and provide logging. It includes an embedded web front end for statistics and the ability to modify configuration settings without restarting the microservice itself.

The library is split in to a few components:

  • Config which provides a structured approach to settings
  • Logger for logging the microservice, including realtime tracing via Regex
  • Stats for realtime instrumentation of the microservice
  • Utils which contains some useful functionality that is used elsewhere in this library
  • Examples

Config

Config reads settings from 2 different files as well as the environment. All settings are specified in the form key=value. The order of precedence for how a setting is defined is:

  1. Environment
  2. settings_local.conf
  3. settings.conf

The settings_local.conf file is normally stored in the same location as the application. settings.local can be stored in the same location, but it is more useful to place this in a parent folder of the application so that some settings can we shared across more than one application.

Gocore offers a number of functions to retrieve settings:

func (c *Configuration) Get(key string, defaultValue ...string) (string, bool) {
func (c *Configuration) GetInt(key string, defaultValue ...int) (int, bool) {
func (c *Configuration) GetBool(key string, defaultValue ...bool) bool {
func (c *Configuration) GetURL(key string, defaultValue ...string) (*url.URL, error, bool) {

depending on the type of value you are interested in. For example:

gocore.Config().Get("database_url") will return a string if the setting is found, or "" if it is not found. The boolean return can be used to check if the setting existed or not.

gocore.Config().GetInt("database_port", 5432) will return an int if the setting is found, or the provided default (5432) if not. The boolean return can be used to check if the setting existed or not.

if gocore.Config().GetBool("happy", false) {
  fmt.Println("I'm happy")
} else {
  fmt.Println("I'm sad")
}

There is also a concept of SETTINGS_CONTEXT which is set via the environment or defaults to "dev" if not.

The general principle is to keep all application settings organised together as it is easier to see differences when they live adjacent to each other and often the same value is used in all different contexts.

The way context is used is probably best explained with an example. Let's specify a settings.conf with multiple versions of one setting in it:

url=http://localhost:8080
url.live=https://www.server.com
url.live.uk=https://www.server.co.uk

and we have an application that reads that setting:

package main

import (
	"fmt"

	"github.com/ordishs/gocore"
)

func main() {
	url, _, _ := gocore.Config().GetURL("url")
	fmt.Printf("URL is %v\n", url)
}

you will now get a different value depending on how you specify SETTINGS_CONTEXT.

go run main.go returns http://localhost:8080

SETTINGS_CONTEXT=live go run main.go returns https://www.server.com

SETTINGS_CONTEXT=live.uk go run main.go returns https://www.server.co.uk

and

SETTINGS_CONTEXT=live.es go run main.go returns https://www.server.com

because there is not SETTINGS_CONTEXT for live.es, and GoCore will keep trying each 'parent' context until it finds an answer. If we ran:

SETTINGS_CONTEXT=stage.eu.red go run main.go

we could get the value of http://localhost:8080 because GoCore would have tried:

  1. url.stage.eu.red - not found
  2. url.stage.eu - not found
  3. url.stage - not found
  4. url - http://localhost:8080

The optional default parameter is useful when you want a sensible value for a setting when it is missing from the settings files and environment:

timeoutMillis, _ := gocore.Config().GetInt("timeout_millis", 30000)

GoCore config will read the environment and settings files one time only. This is done when the first setting is requested. It is useful to write the results of all the settings at application startup, so that you can record them in the application logs. The following example:

package main

import (
	"fmt"

	"github.com/ordishs/gocore"
)

func main() {
	fmt.Printf("STATS\n%s\n-------\n\n", gocore.Config().Stats())
}

will output:

STATS

SETTINGS_CONTEXT
----------------
Not set (dev)

SETTINGS
--------
url=http://localhost:8080

-------

Logger

There are many logging frameworks available and the GoCore logger is very simple implementation with some useful features.

In the examples folder you will see an example of its use.

Documentation

Index

Constants

View Source
const (
	DEBUG logLevel = iota
	INFO
	WARN
	ERROR
	FATAL
	PANIC
)

Variables

View Source
var (
	RootStat = &Stat{
		key:                "root",
		ignoreChildUpdates: true,
	}
)

Functions

func AddAppPayloadFn added in v1.0.33

func AddAppPayloadFn(key string, fn func() interface{})

func CurrentTime added in v1.0.51

func CurrentTime() time.Time

func GetStatPrefix added in v1.0.48

func GetStatPrefix() string

func HandleOther added in v1.0.47

func HandleOther(w http.ResponseWriter, r *http.Request)

func HandleStats added in v1.0.47

func HandleStats(w http.ResponseWriter, r *http.Request)

func NewLogLevelFromString added in v1.0.24

func NewLogLevelFromString(s string) logLevel

func RegisterStatsHandlers added in v1.0.55

func RegisterStatsHandlers()

func ResetStats added in v1.0.47

func ResetStats(w http.ResponseWriter, r *http.Request)

func SetAddress added in v1.0.8

func SetAddress(addr string)

SetAddress comment

func SetInfo added in v1.0.8

func SetInfo(name string, ver string, com string)

SetInfo comment

func StartStatsServer

func StartStatsServer(addr string)

Types

type Configuration

type Configuration struct {
	// contains filtered or unexported fields
}

Configuration comment

func Config

func Config() *Configuration

Config comment

func (*Configuration) Get

func (c *Configuration) Get(key string, defaultValue ...string) (string, bool)

func (*Configuration) GetAll added in v1.0.40

func (c *Configuration) GetAll() map[string]string

func (*Configuration) GetBool

func (c *Configuration) GetBool(key string, defaultValue ...bool) bool

GetBool comment

func (*Configuration) GetContext added in v1.0.36

func (c *Configuration) GetContext() string

Get context

func (*Configuration) GetDuration added in v1.0.58

func (c *Configuration) GetDuration(key string, defaultValue ...time.Duration) (time.Duration, error, bool)

func (*Configuration) GetInt

func (c *Configuration) GetInt(key string, defaultValue ...int) (int, bool)

GetInt comment

func (*Configuration) GetMulti added in v1.0.44

func (c *Configuration) GetMulti(key string, sep string, defaultValue ...[]string) ([]string, bool)

func (*Configuration) GetURL added in v1.0.21

func (c *Configuration) GetURL(key string, defaultValue ...string) (*url.URL, error, bool)

func (*Configuration) Requested added in v1.0.41

func (c *Configuration) Requested() string

func (*Configuration) Set

func (c *Configuration) Set(key string, value string) string

Set an item in the config

func (*Configuration) Stats

func (c *Configuration) Stats() string

Stats comment

func (*Configuration) Unset

func (c *Configuration) Unset(key string) string

Unset removes an item from the config

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger comment

func Log

func Log(packageNameStr string, logLevelOption ...logLevel) *Logger

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

Debug Comment

func (*Logger) Debugf

func (l *Logger) Debugf(msg string, args ...interface{})

Debugf Comment

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

Error comment

func (*Logger) ErrorWithStack

func (l *Logger) ErrorWithStack(msg string, args ...interface{})

ErrorWithStack comment

func (*Logger) Errorf

func (l *Logger) Errorf(msg string, args ...interface{})

Errorf comment

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

Fatal Comment

func (*Logger) Fatalf

func (l *Logger) Fatalf(msg string, args ...interface{})

Fatalf Comment

func (*Logger) GetLogLevel added in v1.0.24

func (l *Logger) GetLogLevel() logLevel

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

Info comment

func (*Logger) Infof

func (l *Logger) Infof(msg string, args ...interface{})

Infof comment

func (*Logger) LogLevel added in v1.0.24

func (l *Logger) LogLevel() int

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

Panic Comment

func (*Logger) Panicf

func (l *Logger) Panicf(msg string, args ...interface{})

Panicf Comment

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

Warn comment

func (*Logger) Warnf

func (l *Logger) Warnf(msg string, args ...interface{})

Warnf comment

type Stat

type Stat struct {
	// contains filtered or unexported fields
}

Stat comment

func NewStat

func NewStat(key string, options ...bool) *Stat

func (*Stat) AddRanges added in v1.0.62

func (s *Stat) AddRanges(ranges ...int)

func (*Stat) AddTime

func (s *Stat) AddTime(startTime time.Time) time.Time

AddTime comment

func (*Stat) AddTimeForRange added in v1.0.61

func (s *Stat) AddTimeForRange(startTime time.Time, sampleSize int) time.Time

func (*Stat) HideTotal added in v1.0.29

func (s *Stat) HideTotal(b bool)

func (*Stat) NewStat added in v1.0.4

func (s *Stat) NewStat(key string, options ...bool) *Stat

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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