mongodbstore

package module
v0.0.0-...-2208494 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: BSD-3-Clause Imports: 11 Imported by: 0

README

mongodbstore

Gorilla's Session store implementation with MongoDB

Forked from github.com/kidstuff/mongostore to migrate from mgo to mongo-go-driver.

Requirements

Depends on the mongo-go-driver library.

Installation

go get github.com/ashulepov/mongodbstore

Documentation

Available on godoc.org.

Example
    func foo(rw http.ResponseWriter, req *http.Request) {
    	// Context
    	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    	
        // Fetch new store.
        client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
        if err != nil {
        	panic(err)
        }
        defer client.Disconnect(ctx)

        store := mongodbstore.NewMongoDBStore(client.Database("test").Collection("test_session"), 3600, true,
            []byte("secret-key"))

        // Get a session.
        session, err := store.Get(req, "session-key")
        if err != nil {
            log.Println(err.Error())
        }

        // Add a value.
        session.Values["foo"] = "bar"

        // Save.
        if err = sessions.Save(req, rw); err != nil {
            log.Printf("Error saving session: %v", err)
        }

        fmt.Fprintln(rw, "ok")
    }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidID = errors.New("mongodbstore: invalid session id")
)

Error definitions

Functions

This section is empty.

Types

type CookieToken

type CookieToken struct{}

func (*CookieToken) GetToken

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

func (*CookieToken) SetToken

func (c *CookieToken) SetToken(rw http.ResponseWriter, name, value string,
	options *sessions.Options)

type MongoDBStore

type MongoDBStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options
	Token   TokenGetSetter
	// contains filtered or unexported fields
}

MongoDBStore stores sessions in MongoDB

func NewMongoDBStore

func NewMongoDBStore(c *mongo.Collection, maxAge int, ensureTTL bool, keyPairs ...[]byte) *MongoDBStore

NewMongoDBStore returns a new MongoDBStore. Set ensureTTL to true let the database auto-remove expired object by maxAge.

func (*MongoDBStore) Get

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

Get registers and returns a session for the given name and session store. It returns a new session if there are no sessions registered for the name.

func (*MongoDBStore) MaxAge

func (m *MongoDBStore) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

func (*MongoDBStore) New

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

New returns a session for the given name without adding it to the registry.

func (*MongoDBStore) Save

func (m *MongoDBStore) 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       primitive.ObjectID `bson:"_id,omitempty"`
	Data     string
	Modified time.Time
}

Session object store in MongoDB

type TokenGetSetter

type TokenGetSetter interface {
	GetToken(req *http.Request, name string) (string, error)
	SetToken(rw http.ResponseWriter, name, value string, options *sessions.Options)
}

Jump to

Keyboard shortcuts

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