configo

package module
v0.0.0-...-e085bf4 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2015 License: Apache-2.0 Imports: 4 Imported by: 0

README

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.

Documentation

Overview

Package configo provides functionality to load an application configuration from TOML files.

It is a shameless wrapper around github.com/BurntSushi/toml that tries to to enforce an environment-based configuration style.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadEnvironment

func LoadEnvironment(env string, v interface{}) error

Loads a specific environment from config.toml file located in the working directory.

func LoadEnvironmentFromFile

func LoadEnvironmentFromFile(fpath string, env string, conf interface{}) error

Loads a specific environment from a specific configuratino file.

Types

Jump to

Keyboard shortcuts

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