mongostore

package module
v3.0.0-...-509cd79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2015 License: MIT Imports: 7 Imported by: 0

README

mongo-session-store

Gorilla's session store implementation using MongoDB

Build Status GoDoc

Installation

Just use go get.

go get gopkg.in/bluesuncorp/mongo-session-store.v3

or to update

go get -u gopkg.in/bluesuncorp/mongo-session-store.v3

And then just import the package into your own code.

import "gopkg.in/bluesuncorp/mongo-session-store.v3"

Usage

Please see http://godoc.org/gopkg.in/bluesuncorp/mongo-session-store.v3 for detailed usage docs.

Contributing

There will be a development branch for each version of this package i.e. v1-development, please make your pull requests against those branches.

If changes are breaking please create an issue, for discussion and create a pull request against the highest development branch for example this package has a v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet.

License

Distributed under MIT License, see license file in code for details.

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")
    }

Index

Constants

This section is empty.

Variables

View Source
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) GetToken

func (c *CookieToken) GetToken(r *http.Request, name string) (string, error)

GetToken returns a Cookie valie and eny errors

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, autoUpdateAccessTime 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. autoUpdateAccessTime - When true the sessions access time will be updated upon every access, even read operations. If false it will only update upon save, or manual intervention within the clients code.

func (*MongoStore) Get

func (m *MongoStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get return a session for the given session name or creates a new one and return it.

func (*MongoStore) New

func (m *MongoStore) New(r *http.Request, name string) (*sessions.Session, error)

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.

type Session

type Session struct {
	ID           bson.ObjectId `bson:"_id,omitempty"`
	Data         string        `bson:"data"`
	LastAccessed *time.Time    `bson:"lastAccessed"`
}

Session struct stored in MongoDB

type TokenGetSeter

type TokenGetSeter interface {
	GetToken(r *http.Request, name string) (string, error)
	SetToken(w http.ResponseWriter, name, value string, options *sessions.Options)
}

TokenGetSeter is the interface for setting and receiving the cookie token

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL