Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

pires/configo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

configo

TOML-based, strong-typed configuration for Go

Build Status GoDoc

Pre-requisites

go get github.com/BurntSushi/toml
github.com/pires/configo

Usage

The configuration file. By default should be placed in the working directory and named application.toml.

[stage]
pubsub = [
  "redisstg1",
  "redisstg2",
  "redisstg3"
]
keepalive = true
heartbeat = "500ms"
timeout = "5s"

[production]
pubsub = [
  "redisprod1",
  "redisprod2"
]
keepalive = true
heartbeat = "200ms"
timeout = "1m"

The code:

package main

import (
        "time"
        "github.com/pires/configo"
)

type configuration struct {
	Pubsub    []string
	Keepalive bool
	Heartbeat *configo.Duration
	Timeout   *configo.Duration
}

var (
	done = make (chan struct{})
)

func main() {
	var config configuration
	if err := configo.LoadEnvironment("stage", &config); err != nil {
		panic(err)
	}

	for _, pubsub := range config.Pubsub {
		println("Connecting to", pubsub)
	}

	if config.Keepalive {
		println("Enabling keep-alive...")
		heartbeat := time.NewTicker(config.Heartbeat.Duration)
		defer heartbeat.Stop()

		// run timeout
		timeout := time.NewTimer(config.Timeout.Duration)
		go func(){
			<-timeout.C
			close(done)
		}()

		// run heartbeat
		for {
			select {
			case <-done:
				println("Done")
				return
			case <- heartbeat.C:
				println("Received heartbeat")
			}
		}
	} else {
		println("Keep-alive disabled.")
	}
}

The output should be:

Connecting to redisstg1
Connecting to redisstg2
Connecting to redisstg3
Enabling keep-alive...
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Received heartbeat
Done

You may have noticed the type configo.Duration instead of time.Duration. This happens because a special kind of unmarshalling is needed. Feel free to implement your own types.

About

TOML-based, strong-typed, environment-oriented, a la fullstack configuration for Go applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages