// TestNotExists validates the ability to load configuration values // using the OS-level environment variables and panic when something // is missing. func TestNotExists(t *testing.T) { t.Log("Given the need to panic when environment variables are missing.") { os.Setenv("MYAPP_PROC_ID", "322") os.Setenv("MYAPP_SOCKET", "./tmp/sockets.po") os.Setenv("MYAPP_PORT", "4034") os.Setenv("MYAPP_FLAG", "true") cfg.Init("MYAPP") t.Log("\tWhen given a namspace key to search for that does NOT exist.") { shouldPanic(t, "STAMP", func() { cfg.MustTime("STAMP") }) shouldPanic(t, "PID", func() { cfg.MustInt("PID") }) shouldPanic(t, "DEST", func() { cfg.MustString("DEST") }) shouldPanic(t, "ACTIVE", func() { cfg.MustBool("ACTIVE") }) shouldPanic(t, "SOCKET_DSN", func() { cfg.MustURL("SOCKET_DSN") }) } } }
// ExampleDev shows how to use the config package. func ExampleDev() { // Init() must be called only once with the given namespace to load. cfg.Init(cfg.MapProvider{ Map: map[string]string{ "IP": "40.23.233.10", "PORT": "4044", "INIT_STAMP": time.Date(2009, time.November, 10, 15, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate), "FLAG": "on", }, }) // To get the ip. fmt.Println(cfg.MustString("IP")) // To get the port number. fmt.Println(cfg.MustInt("PORT")) // To get the timestamp. fmt.Println(cfg.MustTime("INIT_STAMP")) // To get the flag. fmt.Println(cfg.MustBool("FLAG")) // Output: // 40.23.233.10 // 4044 // 2009-11-10 15:00:00 +0000 UTC // true }
// ExampleDev shows how to use the config package. func ExampleDev() { // Set up some basic environment variables. os.Setenv("DOCK_IP", "40.23.233.10") os.Setenv("DOCK_PORT", "4044") os.Setenv("DOCK_INIT_STAMP", time.Date(2009, time.November, 10, 15, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate)) os.Setenv("DOCK_FLAG", "true") // Init() must be called only once with the given namespace to load. cfg.Init("DOCK") // To get the ip. fmt.Println(cfg.MustString("IP")) // To get the port number. fmt.Println(cfg.MustInt("PORT")) // To get the timestamp. fmt.Println(cfg.MustTime("INIT_STAMP")) // To get the flag. fmt.Println(cfg.MustBool("FLAG")) // Output: // 40.23.233.10 // 4044 // 2009-11-10 15:00:00 +0000 UTC // true }
// Init sets up the MongoDB environment. This expects that the // cfg package has been initialized first. func Init() error { m.mu.Lock() defer m.mu.Unlock() if m.ses != nil { return nil } // We need this object to establish a session to our MongoDB. mongoDBDialInfo := mgo.DialInfo{ Addrs: []string{cfg.MustString("MONGO_HOST")}, Timeout: 60 * time.Second, Database: cfg.MustString("MONGO_AUTHDB"), Username: cfg.MustString("MONGO_USER"), Password: cfg.MustString("MONGO_PASS"), } // Create a session which maintains a pool of socket connections // to our MongoDB. var err error if m.ses, err = mgo.DialWithInfo(&mongoDBDialInfo); err != nil { return err } // Save the database name to use. m.dbName = cfg.MustString("MONGO_DB") // Reads may not be entirely up-to-date, but they will always see the // history of changes moving forward, the data read will be consistent // across sequential queries in the same session, and modifications made // within the session will be observed in following queries (read-your-writes). // http://godoc.org/labix.org/v2/mgo#Session.SetMode m.ses.SetMode(mgo.Monotonic, true) return nil }
func main() { log.User("startup", "Init", "Revision : %q", GitRevision) log.User("startup", "Init", "Version : %q", GitVersion) log.User("startup", "Init", "Build Date : %q", BuildDate) log.User("startup", "Init", "Int Version : %q", IntVersion) log.User("startup", "Init", "Go Version : %q", runtime.Version()) log.User("startup", "Init", "Go Compiler : %q", runtime.Compiler) log.User("startup", "Init", "Go ARCH : %q", runtime.GOARCH) log.User("startup", "Init", "Go OS : %q", runtime.GOOS) handlers.Version.GitRevision = GitRevision handlers.Version.GitVersion = GitVersion handlers.Version.BuildDate = BuildDate handlers.Version.IntVersion = IntVersion // These are the absolute read and write timeouts. const ( // ReadTimeout covers the time from when the connection is accepted to when the // request body is fully read. readTimeout = 10 * time.Second // WriteTimeout normally covers the time from the end of the request header read // to the end of the response write. writeTimeout = 30 * time.Second ) host := cfg.MustString(cfgHost) log.User("startup", "Init", "Binding web service to %s", host) if err := web.Run(host, routes.API(), readTimeout, writeTimeout); err != nil { log.Error("shutdown", "Init", err, "App Shutdown") os.Exit(1) } log.User("shutdown", "Init", "App Shutdown") }
// TestNotExists validates the ability to load configuration values // using the OS-level environment variables and panic when something // is missing. func TestNotExists(t *testing.T) { t.Log("Given the need to panic when environment variables are missing.") { cfg.Init(cfg.MapProvider{ Map: map[string]string{ "PROC_ID": "322", "SOCKET": "./tmp/sockets.po", "PORT": "4034", "FLAG": "on", }, }) t.Log("\tWhen given a namspace key to search for that does NOT exist.") { shouldPanic(t, "STAMP", func() { cfg.MustTime("STAMP") }) shouldPanic(t, "PID", func() { cfg.MustInt("PID") }) shouldPanic(t, "DEST", func() { cfg.MustString("DEST") }) shouldPanic(t, "ACTIVE", func() { cfg.MustBool("ACTIVE") }) shouldPanic(t, "SOCKET_DSN", func() { cfg.MustURL("SOCKET_DSN") }) } } }