Help us shape the future of Go! Take the 2026 Go Developer Survey

mongostore

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: BSD-3-Clause Imports: 10 Imported by: 5

README

mongostore

CodeQL Run Tests Go Report Card GoDoc codecov

Gorilla's Session store implementation with MongoDB

Requirements

Depends on the mgo library.

Installation

For the latest go version, run:

go get github.com/laziness-coders/mongostore

For the go version 1.20 and under, run:

go get github.com/laziness-coders/mongostore@v0.0.6

Documentation

Available on godoc.org.

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

        // Get a session.
        store := NewMongoStore(
            client.Database("test").Collection("test_session"),
            3600,
            false,
            []byte("secret-key"),
        )

        // 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("mgostore: invalid session id")

Functions

This section is empty.

Types

type CookieToken

type CookieToken struct{}

func (*CookieToken) GetToken

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

func (*CookieToken) SetToken

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

type MongoStore

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

MongoStore stores sessions in MongoDB

func NewMongoStore

func NewMongoStore(c *mongo.Collection, 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.

func (*MongoStore) Get

func (m *MongoStore) 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 (*MongoStore) MaxAge

func (m *MongoStore) 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 (*MongoStore) MaxLength added in v0.0.15

func (m *MongoStore) MaxLength(l int)

MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new Store is 4096.

func (*MongoStore) New

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

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

func (*MongoStore) Save

func (m *MongoStore) Save(_ *http.Request, w http.ResponseWriter,
	session *sessions.Session,
) error

Save saves all sessions registered for the current request.

type Session

type Session struct {
	Id       string `bson:"_id,omitempty"`
	Data     string
	Modified time.Time
}

Session object store in MongoDB

type TokenGetSeter

type TokenGetSeter 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