config

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: Apache-2.0, MIT Imports: 7 Imported by: 0

README

snakelet

Ultra minimal go config lib based on viper.

Alternatively, the source code itself can be used for inspiration as to how to use viper internally.

Usage

import (
	snakelet "github.com/CIDgravity/snakelet"
)

type DatabaseConfig struct {
	User     string `mapstructure:"user" validate:"required"`
	Password string `mapstructure:"password" validate:"required"`
	Host     string `mapstructure:"host" validate:"required"`
	Port     int    `mapstructure:"port" validate:"required"`
	Name     string `mapstructure:"name" validate:"required"`
	SslMode  string `mapstructure:"sslMode"`
	MaxConns int    `mapstructure:"maxConns"`
}

type LogsConfig struct {
	LogLevel                string `mapstructure:"level"` // error | warn | info - case insensitive
	BackendLogsToJsonFormat bool   `mapstructure:"isJSON"`
	DatabaseSlowThreshold   string `mapstructure:"databaseSlowThreshold"` // Value under which a query is considered slow. "1ms", "1s", etc - anything that's parsable by time.ParseDuration(interval).
}

type Config struct {
	Database DatabaseConfig `mapstructure:"database"`
	Logs     LogsConfig     `mapstructure:"log"`
	Server   ServerConfig   `mapstructure:"server"`
}

type ServerConfig struct {
	Port int `mapstructure:"port" validate:"required"`
}

// only set default for non-required tag
func GetDefaultConfig() *Config {
	return &Config{
		Database: DatabaseConfig{
			SslMode:  "disable",
			MaxConns: 200,
		},
		Logs: LogsConfig{
			LogLevel:                "debug",
			BackendLogsToJsonFormat: false,
			DatabaseSlowThreshold:   "1ms",
		},
	}

}


// Get config struct and default variables
conf := GetDefaultConfig()
// this will mutate the `conf` variable:
err := snakelet.InitAndLoad(conf, "/opt/company/project.yaml", "company-project")
if err != nil {
	return fmt.Errorf("Unable to init and load config: %w", err)
}
// Pass `conf` or a subset of this variable as param throughout the rest of the code so you don't have any dependency on snakelet or viper whatsoever.
// This also contributes to improving testability of your code (you can code with pure funcitons):
// someServerComponent := server.NewServer(conf.Server)
// someDbComponent := database.NewDatabase(conf.Database)

Also checkout ./config_test.go and InitAndLoadWithLogger.

License

All this work is licensed under permissive open-source license.

Except written otherwise in particular files, this work is dual-licensed under both Apache v2 and MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug()

Only used for debug purpose during development (print to standard output) DO NOT USE IT IN PRODUCTION OR CRITICAL INFORMATION FROM CONF MAY BE STORED IN LOG SERVERS WARNING: this will print the entire configuration!

func InitAndLoad

func InitAndLoad(configStruct interface{}, cfgFile string, prefix string) error

func InitAndLoadWithLogger

func InitAndLoadWithLogger(configStruct interface{}, cfgFile string, prefix string, logger Logger) error

Prefix must be unique in between each projects. Env variables are only set if a prefix has been set if cfgFile == "", it will used config.yaml in the directory where the executable is located. else it will use cfgFile (full path required)

Types

type DefaultLogger

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

func (*DefaultLogger) Printf

func (p *DefaultLogger) Printf(format string, v ...any)

type Logger

type Logger interface {
	Printf(format string, a ...any)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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