Documentation ¶
Overview ¶
Mongo Session Store follows the Gorilla Session implementation
Reason for creation over using an existing library
When the MongoDB database the sessions were being stored in was not reachable especially in the event of a database cycle other libraries would not restablish the database session; this library will.
Example Usage:
func foo(w http.ResponseWriter, r *http.Request) { // Coonect to MongoDB dbSess, err := mgo.Dial("localhost") if err != nil { panic(err) } defer dbSess.Close() store := mongostore.NewMongoStore(dbSess, "sessions", 3600, true, []byte("secret-key")) // Get a session. session, err := store.Get(r, "session-key") if err != nil { log.Println(err.Error()) } // Add a value. session.Values["foo"] = "bar" // Save. if err = sessions.Save(r, w); err != nil { log.Printf("Error saving session: %v", err) } fmt.Fprintln(w, "ok") }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidID error when Session ID is invalid ErrInvalidID = errors.New("Invalid Session ID") )
Functions ¶
This section is empty.
Types ¶
type CookieToken ¶
type CookieToken struct{}
CookieToken struct
func (*CookieToken) SetToken ¶
func (c *CookieToken) SetToken(w http.ResponseWriter, name, value string, options *sessions.Options)
SetToken sets the sessions cookie
type MongoStore ¶
type MongoStore struct { Codecs []securecookie.Codec Options *sessions.Options Token TokenGetSeter // contains filtered or unexported fields }
MongoStore struct contains options and variables to interact with session settings
func NewMongoStore ¶
func NewMongoStore(s *mgo.Session, collectionName string, maxAge int, ensureTTL bool, keyPairs ...[]byte) *MongoStore
NewMongoStore returns a new MongoStore. Set ensureTTL to true let the database auto-remove expired object by maxAge. This is using *mgo.Session instead of *.mgo.Collection because if the database goes offline and then comes back onlne we will need the session to refresh the database connection.
func (*MongoStore) Get ¶
Get return a session for the given session name or creates a new one and return it.
func (*MongoStore) New ¶
New returns a session for the given session name without adding it to the registry.
func (*MongoStore) Save ¶
func (m *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
Save saves all sessions registered for the current request.