config

package
v0.0.0-...-f42576b Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// EmptyConfigurationForError Represents the configuration instance to be
	// used when there is a configuration error during load
	EmptyConfigurationForError = &Config{}
	// ConfigFilename is the default file name when nothing is provided
	ConfigFilename = "appconfig.cfg"
	// DefaultConfiguration is the configuration that will be in effect if no configuration is loaded from any of the expected locations
	DefaultConfiguration = `` /* 343-byte string literal not displayed */

	// DefaultLoadFunc defines
	DefaultLoadFunc   = GetLoadFunc(DefaultConfiguration, ConfigFilename, "/etc/appconfig/", "/.appconfig/")
	LoadConfiguration = DefaultLoadFunc
)
View Source
var (
	ParseCLIArgs = func(programName string, args []string) (cliConfig *CLIConfig, output string, err error) {
		flags := flag.NewFlagSet(programName, flag.ContinueOnError)
		var buf bytes.Buffer
		flags.SetOutput(&buf)

		var conf CLIConfig
		flags.StringVar(&conf.ConfigPath, "config", "", "Config file location")
		flags.StringVar(&conf.MigrationSource, "migrate", "", "Migration source folder")
		flags.BoolVar(&conf.StopOnConfigChange, "stop-on-conf-change", false, "Restart internally on -config change if this flag is absent")
		flags.BoolVar(&conf.DoNotWatchConfigChange, "do-not-watch-conf-change", false, "Do not watch config change")

		err = flags.Parse(args)
		if err != nil {
			return nil, buf.String(), err
		}

		if len(conf.MigrationSource) > 0 {
			fileInfo, err := os.Stat(conf.MigrationSource)
			if err != nil {
				return nil, "Could not determine migration source details", err
			}
			if !fileInfo.IsDir() {
				return nil, "Migration source must be a dir", errMigrationSrcNotDir
			}
			if !filepath.IsAbs(conf.MigrationSource) {
				conf.MigrationSource, _ = filepath.Abs(conf.MigrationSource)
			}
			conf.MigrationSource = "file://" + conf.MigrationSource
		}

		return &conf, buf.String(), nil
	}
)

Functions

func GetLoadFunc

func GetLoadFunc(defaultConfig, configFilename, systemPathPrefix, userHomePathPrefix string) func(string) (*ini.File, error)

GetLoadFunc provides an API for wrapping multi-level configuration loading for any application trying to load function

Types

type CLIConfig

type CLIConfig struct {
	ConfigPath             string
	MigrationSource        string
	StopOnConfigChange     bool
	DoNotWatchConfigChange bool
	// contains filtered or unexported fields
}

CLIConfig represents the Command Line Args config

func (*CLIConfig) IsConfigWatcherStarted

func (conf *CLIConfig) IsConfigWatcherStarted() bool

IsConfigWatcherStarted returns whether config watcher is running

func (*CLIConfig) IsMigrationEnabled

func (conf *CLIConfig) IsMigrationEnabled() bool

IsMigrationEnabled returns whether migration is enabled

func (*CLIConfig) NotifyOnConfigFileChange

func (conf *CLIConfig) NotifyOnConfigFileChange(callback func())

NotifyOnConfigFileChange registers a callback function for changes to ConfigPath; it calls the `callback` when a change is detected

func (*CLIConfig) StopWatcher

func (conf *CLIConfig) StopWatcher()

StopWatcher stops any watcher if started for CLI ConfigPath file change

type Config

type Config struct {
	DBDialect               DBDialect
	DBConnectionURL         string
	DBConnectionMaxIdleTime time.Duration
	DBConnectionMaxLifetime time.Duration
	DBMaxIdleConnections    uint16
	DBMaxOpenConnections    uint16
	HTTPListeningAddr       string
	HTTPReadTimeout         time.Duration
	HTTPWriteTimeout        time.Duration
	LogFilename             string
	MaxFileSize             uint
	MaxBackups              uint
	MaxAge                  uint
	CompressBackupsEnabled  bool
	LogLevel                LogLevel
}

Config represents the application configuration

func GetAutoConfiguration

func GetAutoConfiguration() (*Config, *ini.File, error)

GetAutoConfiguration gets configuration from default config and system defined path chain of /etc/webhook-broker/webhook-broker.cfg, {USER_HOME}/.webhook-broker/webhook-broker.cfg, webhook-broker.cfg (current dir)

func GetConfiguration

func GetConfiguration(configFilePath string) (*Config, *ini.File, error)

GetConfiguration gets the current state of application configuration

func GetConfigurationFromCLIConfig

func GetConfigurationFromCLIConfig(cliConfig *CLIConfig) (*Config, *ini.File, error)

GetConfigurationFromCLIConfig from CLIConfig.

func GetConfigurationFromParseConfig

func GetConfigurationFromParseConfig(cfg *ini.File) (*Config, error)

GetConfigurationFromParseConfig returns configuration from parsed configuration

func (*Config) GetDBConnectionMaxIdleTime

func (config *Config) GetDBConnectionMaxIdleTime() time.Duration

GetDBConnectionMaxIdleTime returns the DB Connection max idle time

func (*Config) GetDBConnectionMaxLifetime

func (config *Config) GetDBConnectionMaxLifetime() time.Duration

GetDBConnectionMaxLifetime returns the DB Connection max lifetime

func (*Config) GetDBConnectionURL

func (config *Config) GetDBConnectionURL() string

GetDBConnectionURL returns the DB Connection URL string

func (*Config) GetDBDialect

func (config *Config) GetDBDialect() DBDialect

GetDBDialect returns the DB dialect of the configuration

func (*Config) GetHTTPListeningAddr

func (config *Config) GetHTTPListeningAddr() string

GetHTTPListeningAddr retrieves the connection string to listen to

func (*Config) GetHTTPReadTimeout

func (config *Config) GetHTTPReadTimeout() time.Duration

GetHTTPReadTimeout retrieves the connection read timeout

func (*Config) GetHTTPWriteTimeout

func (config *Config) GetHTTPWriteTimeout() time.Duration

GetHTTPWriteTimeout retrieves the connection write timeout

func (*Config) GetLogFilename

func (config *Config) GetLogFilename() string

GetLogFilename retrieves the file name of the log

func (*Config) GetLogLevel

func (config *Config) GetLogLevel() LogLevel

GetLogLevel returns the log level as per the configuration

func (*Config) GetMaxAgeForALogFile

func (config *Config) GetMaxAgeForALogFile() uint

GetMaxAgeForALogFile retrieves maximum day to retain a rotated log file

func (*Config) GetMaxIdleDBConnections

func (config *Config) GetMaxIdleDBConnections() uint16

GetMaxIdleDBConnections returns the maximum number of idle DB connections to retain in pool

func (*Config) GetMaxLogBackups

func (config *Config) GetMaxLogBackups() uint

GetMaxLogBackups retrieves max rotated logs to retain

func (*Config) GetMaxLogFileSize

func (config *Config) GetMaxLogFileSize() uint

GetMaxLogFileSize retrieves the max log file size before its rotated in MB

func (*Config) GetMaxOpenDBConnections

func (config *Config) GetMaxOpenDBConnections() uint16

GetMaxOpenDBConnections returns the maximum number of concurrent DB connections to keep open

func (*Config) IsCompressionEnabledOnLogBackups

func (config *Config) IsCompressionEnabledOnLogBackups() bool

IsCompressionEnabledOnLogBackups checks if log backups are compressed

func (*Config) IsLoggerConfigAvailable

func (config *Config) IsLoggerConfigAvailable() bool

IsLoggerConfigAvailable checks is logger configuration is set since its optional

type DBDialect

type DBDialect string

DBDialect allows us to define constants for supported DB drivers

type HTTPConfig

type HTTPConfig interface {
	GetHTTPListeningAddr() string
	GetHTTPReadTimeout() time.Duration
	GetHTTPWriteTimeout() time.Duration
}

HTTPConfig represents the HTTP configuration related behaviors

type LogConfig

type LogConfig interface {
	GetLogLevel() LogLevel
	IsLoggerConfigAvailable() bool
	GetLogFilename() string
	GetMaxLogFileSize() uint
	GetMaxLogBackups() uint
	GetMaxAgeForALogFile() uint
	IsCompressionEnabledOnLogBackups() bool
}

LogConfig represents the interface for log related configuration

type LogLevel

type LogLevel uint8

LogLevel represents the log level logger should use

const (
	// SQLite3Dialect represents the DB Dialect for SQLite3
	SQLite3Dialect = DBDialect("sqlite3")
	// MySQLDialect represents the DB Dialect for MySQL
	MySQLDialect = DBDialect("mysql")
	// Debug is the lowest LogLevel, will expose all logs
	Debug LogLevel = 20 + iota
	// Info is the second lowest LogLevel
	Info
	// Error is the second highest LogLevel
	Error
	// Fatal is the highest LogLevel with lowest logs
	Fatal
)

type RelationalDatabaseConfig

type RelationalDatabaseConfig interface {
	GetDBDialect() DBDialect
	GetDBConnectionURL() string
	GetDBConnectionMaxIdleTime() time.Duration
	GetDBConnectionMaxLifetime() time.Duration
	GetMaxIdleDBConnections() uint16
	GetMaxOpenDBConnections() uint16
}

RelationalDatabaseConfig represents DB configuration related behaviors

Jump to

Keyboard shortcuts

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