<repo_name>PeachIceTea/nigroni-mgo-session<file_sep>/nms.go
package nigronimgosession

import "github.com/globalsign/mgo"

type contextKey int

// Define keys that support equality.
const KEY contextKey = 0

type NMS struct {
	DB      *mgo.Database
	Session *mgo.Session
}
<file_sep>/database_accessor.go
package nigronimgosession

import (
	"context"
	"net/http"

	mgo "github.com/globalsign/mgo"
)

type DatabaseAccessor struct {
	*mgo.Session
	url  string
	name string
	coll string
}

func NewDatabaseAccessor(url, name, coll string) (*DatabaseAccessor, error) {
	session, err := mgo.Dial(url)
	if err == nil {
		return &DatabaseAccessor{session, url, name, coll}, nil
	} else {
		return &DatabaseAccessor{}, err
	}
}

func (da *DatabaseAccessor) Set(ctx context.Context, request *http.Request, session *mgo.Session) context.Context {
	db := session.DB(da.name)
	nms := &NMS{db, session}
	return context.WithValue(ctx, KEY, nms)
}
