Exemple #1
0
// InitMGO sets up the MongoDB environment. This expects that the
// cfg package has been initialized first.
func InitMGO() error {
	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
}
// 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
}
Exemple #3
0
// 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")
			})
		}
	}
}