Documentation ¶
Overview ¶
Package mongostore 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.New(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") }
Updating Cookie and MongoDB Expiry Times
if you need your cookies to be rolling and update with every access and not just modifications you can call: // Get a session. session, err := store.GetAndUpdateAccessTime(r, w, "session-key") instead of // Get a session. session, err := store.Get(r, "session-key")
Index ¶
- Variables
- type CookieToken
- type MongoStore
- func (m *MongoStore) Get(r *http.Request, name string) (*sessions.Session, error)
- func (m *MongoStore) GetAndUpdateAccessTime(r *http.Request, w http.ResponseWriter, name string) (*sessions.Session, error)
- func (m *MongoStore) New(r *http.Request, name string) (*sessions.Session, error)
- func (m *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error
- type Session
- type TokenGetSeter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidID error when Session ID is invalid ErrInvalidID = errors.New("Invalid Session ID") // ErrInvalidAccessTime error when the Last Accessed Time is invalid ErrInvalidAccessTime = errors.New("Invalid Last Accessed Time") )
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 New ¶
func New(s *mgo.Session, collectionName string, options *sessions.Options, ensureTTL bool, keyPairs ...[]byte) *MongoStore
New 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. If false it will only update upon save, or manual intervention within the clients code.
func (*MongoStore) Get ¶
Get returns a session for the given session name or creates a new one and return it.
func (*MongoStore) GetAndUpdateAccessTime ¶
func (m *MongoStore) GetAndUpdateAccessTime(r *http.Request, w http.ResponseWriter, name string) (*sessions.Session, error)
GetAndUpdateAccessTime returns a session for the given session name or creates a new one and if exists updates it's lastAccessed time
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.