config

package
v1.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 19 Imported by: 0

README

Config pakcage

This package will make a global config management package for an app. This package work wil component who's will make the real intelligence of the config. For example, the main config file is not existing into this config, because is generated by the sum of all component config part.

Exmaple of implement

You can follow the initialization define into the cobra package Here we will define the intialization of the config package.

Main init of config :

Make a package's file to be called by yours application command or routers Into this file we will use some import

import (
    "fmt"
    "net/http"

    liberr "github.com/nabbar/golib/errors"
    libsts "github.com/nabbar/golib/status"
    liblog "github.com/nabbar/golib/logger"
    librtr "github.com/nabbar/golib/router"
	
    cmphea "github.com/nabbar/golib/config/components/head"
    cmphtp "github.com/nabbar/golib/config/components/http"
    cmplog "github.com/nabbar/golib/config/components/log"
    cmptls "github.com/nabbar/golib/config/components/tls"

    compkg "../pkg"
)

Some constant to prevents error on copy/paste :

const (
    ConfigKeyHead = "headers"
    ConfigKeyHttp = "servers"
    ConfigKeyLog  = "log"
    ConfigKeyTls  = "tls"
)

This function will init the libs and return the lib resource. This is an example, but you can create your own component to add it into you config. The config can have multi instance of same component type but with different config key. As That, you can for example define a TLS config for HTTP server, a TLS config for a client, ...

func SetConfig(lvl liblog.Level, handler map[string]http.Handler) {
    compkg.GetConfig().ComponentSet(ConfigKeyHead, cmphea.New())
    compkg.GetConfig().ComponentSet(ConfigKeyLog, cmplog.New(lvl, compkg.GetLogger))
    compkg.GetConfig().ComponentSet(ConfigKeyTls, cmptls.New())
    compkg.GetConfig().ComponentSet(ConfigKeyHttp, cmphtp.New(_ConfigKeyTls, _ConfigKeyLog, handler))
}

// This function will return the router header based of the component header stored into the config
func GetHeader() librtr.Headers {
    if cmp := cmphea.Load(compkg.GetConfig().ComponentGet, _ConfigKeyHead); cmp != nil {
        return cmp.GetHeaders()
    } else {
        GetLog().Fatal("component '%s' is not initialized", _ConfigKeyHead)
        return nil
    }
}

// This function will return the logger for the application. 
// If the component is not started, this function will return the root logger  
func GetLog() liblog.Logger {
    if !compkg.GetConfig().ComponentIsStarted() {
        return nil
    }

    if cmp := cmplog.Load(compkg.GetConfig().ComponentGet, _ConfigKeyLog); cmp != nil {
        return cmp.Log()
    } else {
        compkg.GetLogger().Error("component '%s' is not initialized", _ConfigKeyLog)
        return compkg.GetLogger()
    }
}

This function will connect the status package of golib with the httpserver package stored into the config :

// This function can be call by the implementation of Status Package to expose the status of the pool web server
// This function will update the status after each reload the pool web server.
func SetRouteStatus(sts libsts.RouteStatus) {
    compkg.GetConfig().RegisterFuncReloadAfter(func() liberr.Error {
        var cmp cmphtp.ComponentHttp

		// If component has not been started, skill the function
        if !compkg.GetConfig().ComponentIsStarted() {
            return nil
        }

		// if cannot access to the Http component, generate an error
        if cmp = cmphtp.Load(compkg.GetConfig().ComponentGet, ConfigKeyHttp); cmp == nil {
            return ErrorComponentNotInitialized.ErrorParent(fmt.Errorf("component '%s'", ConfigKeyHttp))
        }

		// Get the Pool Server
        pool := cmp.GetPool()
        pool.StatusRoute(_ConfigKeyHttp, GetRouteStatusMessage, sts)
		
		// Don't forget to update the component pool
        cmp.SetPool(pool)

        return nil
    })
}

To generate the config example, just call this function :

    compkg.GetConfig().DefaultConfig()

This function will associate the config Key with the component default config into a main buffer to return it. As that, config can be extended easily with many component and the config is automatically managed.

Documentation

Index

Constants

View Source
const (
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgConfig
	ErrorConfigMissingViper
	ErrorComponentNotFound
	ErrorComponentFlagError
	ErrorComponentConfigNotFound
	ErrorComponentConfigError
	ErrorComponentStart
	ErrorComponentReload
)
View Source
const (
	MinErrorComponentAws      = ErrorParamEmpty + 10
	MinErrorComponentDatabase = MinErrorComponentAws + 10
	MinErrorComponentHead     = MinErrorComponentDatabase + 10
	MinErrorComponentHttp     = MinErrorComponentHead + 10
	MinErrorComponentLdap     = MinErrorComponentHttp + 10
	MinErrorComponentLog      = MinErrorComponentLdap + 10
	MinErrorComponentMail     = MinErrorComponentLog + 10
	MinErrorComponentNats     = MinErrorComponentMail + 10
	MinErrorComponentNutsDB   = MinErrorComponentNats + 10
	MinErrorComponentRequest  = MinErrorComponentNutsDB + 10
	MinErrorComponentSmtp     = MinErrorComponentRequest + 10
	MinErrorComponentTls      = MinErrorComponentSmtp + 10
)

Variables

This section is empty.

Functions

func Shutdown added in v1.10.0

func Shutdown()

func WaitNotify added in v1.10.0

func WaitNotify()

Types

type Config

type Config interface {

	// Context return the config context instance
	Context() libctx.Config[string]

	// CancelAdd allow to register a slice of custom function called on cancel context.
	// On context cancel event or signal kill, term... this function will be called
	// before config stop and main context cancel function
	CancelAdd(fct ...func())

	// CancelClean allow clear the all Cancel func registered into slice
	CancelClean()

	// Start will trigger the start function of all registered component.
	// If any component return an error, this func will stop the start
	// process and return the error.
	Start() liberr.Error

	// Reload triggers the Reload function of each registered Component.
	Reload() liberr.Error

	// Stop will trigger the stop function of all registered component.
	// All component must stop cleanly.
	Stop()

	// Shutdown will trigger all stop function.
	// This function will call the Stop function and the private function cancel.
	// This will stop all process and do like a SIGTERM/SIGINT signal.
	// This will finish by an os.Exit with the given parameter code.
	Shutdown(code int)

	// RegisterFuncViper is used to expose golib Viper instance to all config component.
	// With this function, the component can load his own config part and start or reload.
	RegisterFuncViper(fct libvpr.FuncViper)

	// RegisterFuncStartBefore allow to register a func to be call when the config Start
	// is trigger. This func is call before the start sequence.
	RegisterFuncStartBefore(fct FuncEvent)

	// RegisterFuncStartAfter allow to register a func to be call when the config Start
	// is trigger. This func is call after the start sequence.
	RegisterFuncStartAfter(fct FuncEvent)

	// RegisterFuncReloadBefore allow to register a func to be call when the config Reload
	// is trigger. This func is call before the reload sequence.
	RegisterFuncReloadBefore(fct FuncEvent)

	// RegisterFuncReloadAfter allow to register a func to be call when the config Reload
	// is trigger. This func is call after the reload sequence.
	RegisterFuncReloadAfter(fct FuncEvent)

	// RegisterFuncStopBefore allow to register a func to be call when the config Stop
	// is trigger. This func is call before the stop sequence.
	RegisterFuncStopBefore(fct FuncEvent)

	// RegisterFuncStopAfter allow to register a func to be call when the config Stop
	// is trigger. This func is call after the stop sequence.
	RegisterFuncStopAfter(fct FuncEvent)

	// RegisterDefaultLogger allow to register a func to return a default logger.
	// This logger can be used by component to extend config ot to log message.
	RegisterDefaultLogger(fct liblog.FuncLog)

	/*
		// Section Component : github.com/nabbar/golib/config
	*/
	cfgtps.ComponentList
	cfgtps.ComponentMonitor
}

func New

func New(vrs libver.Version) Config

type FuncEvent added in v1.10.0

type FuncEvent func() liberr.Error

Directories

Path Synopsis
components
aws
log
tls

Jump to

Keyboard shortcuts

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