Source

devsrvr / src / config.go

Full commit
package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"path"
	"time"
)

// data type for config information
type Config struct {
	DevServerAddress string
	AppAddress       string
	App              string
	CrashTimeout     time.Duration
	StartupTime      time.Duration
	LogFile          string
}

// load a json config file with details about the app we're running
func LoadConfig(configpath string) *Config {
	b, err := ioutil.ReadFile(path.Join(configpath, "app.json"))
	if err != nil {
		log.Fatal(err.Error())
	}

	// config file defaults, overwritten by anything in the real file
	conf := &Config{
		"127.0.0.1:8080", // DevServerAddress
		"127.0.0.1:8081", // AppAddress
		"",               // App
		100000000,        // CrashTimeout
		time.Second / 2,  // StartupTime
		"",               // LogFile
	}

	if err = json.Unmarshal(b, &conf); err != nil {
		log.Fatal("ReadFile: ", err.Error())
	}

	if conf.App == "" {
		log.Fatal("must include 'App' in your app.json")
	}

	return conf
}