mongostore

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 11 Imported by: 0

README

go-mongo-store

Gorilla's Session store implementation with MongoDB

Requirements

Depends on the qmgo library.

Installation

go get github.com/p000ic/go-mongo-store

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidId = errors.New("mongo-store: invalid session id")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Host          string
	Port          int
	Source        string
	Collection    string
	AuthMechanism string
	Username      string
	Password      string
	AuthSource    string
	Auth          bool
}

Config mongodb configuration parameters

func NewConfig

func NewConfig(host, source, collection, username, password, authSource string, port int) *Config

NewConfig create mongodb configuration

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 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 *qmgo.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) 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(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             `bson:"data"`
	Modified time.Time          `bson:"modified"`
}

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