Skip to content

Config package for golang that makes it easy to read environment variables and yaml files.

Notifications You must be signed in to change notification settings

Santiclause/goconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Configuration

This package just makes it simpler to deal with configuration. Just define a struct with the appropriate tags, and either embed the config.Config struct type or manually implement the interface (I recommend the former), and then you're essentially good to go. Observe:

package main
import (
    "time"
    "github.com/santiclause/goconfig"
)
type Config struct {
	HttpPort            int           `yaml:"http_port" env:"HTTP_PORT"`
	ConnTimeout         time.Duration `yaml:"conn_timeout" env:"CONN_TIMEOUT" required:"true"`
	RequestGracePeriod  time.Duration `yaml:"request_grace_period" env:"REQUEST_GRACE_PERIOD"`
	goconfig.Config
}

func main() {
    // Make the struct, with a default value (since Load will overwrite it)
    config := &Config{
        HttpPort: 8080,
    }
    config.SetFilename("whatever.yaml")
    goconfig.Load(config)
    // Optionally make it listen for SIGHUPs
    goconfig.ListenForSignals(config)
}

Supported tags

Priority

The provided yaml file will be loaded first (if it exists), and environment variables will override the yaml files.

Debug level

The Config struct type also defines a debug level and reads it from yaml: debug and env: DEBUG respectively, and you can check if the debug level is at least at a certain level via config.DebugLevel(level) - e.g.:

if config.DebugLevel("info") {
    log.Println("New connection from client")
}

About

Config package for golang that makes it easy to read environment variables and yaml files.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages