mongodbstoregorilla

package module
v0.0.0-...-84372f8 Latest Latest
Warning

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

Go to latest
Published: May 23, 2020 License: MIT Imports: 11 Imported by: 1

README

gorilla-sessions-mongodb

Gorilla's Session store implementation for mongoDB using official Go driver

Installation

go get github.com/2-72/gorilla-sessions-mongodb
Example
package main

import (
	"context"
	"log"
	"net/http"
	"os"

	mongodbstore "github.com/2-72/gorilla-sessions-mongodb"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {

	client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		log.Printf("error connecting to mongodb %v \n", err)
		return
	}
	coll := client.Database("app_db").Collection("sessions")
	store, err := mongodbstore.NewMongoDBStore(coll, []byte(os.Getenv("SESSION_KEY")))
	if err != nil {
		log.Printf("error initializing mongodb store %v \n", err)
		return
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		err = session.Save(r, w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MongoDBStore

type MongoDBStore struct {
	// contains filtered or unexported fields
}

MongoDBStore stores sessions using mongoDB as backend.

func NewMongoDBStore

func NewMongoDBStore(col *mongo.Collection, keyPairs ...[]byte) (*MongoDBStore, error)

NewMongoDBStore returns a new NewMongoDBStore with default config

defaultConfig := MongoDBStoreConfig{
	IndexTTL: true,
	SessionOptions: sessions.Options{
		Path:     "/",
		MaxAge:   3600 * 24 * 30,
		HttpOnly: true,
	},
}

func NewMongoDBStoreWithConfig

func NewMongoDBStoreWithConfig(coll *mongo.Collection, cfg MongoDBStoreConfig, keyPairs ...[]byte) (*MongoDBStore, error)

NewMongoDBStoreWithConfig returns a new NewMongoDBStore with a custom MongoDBStoreConfig

func (*MongoDBStore) Get

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

Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded.

func (*MongoDBStore) New

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

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

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call.

func (*MongoDBStore) Save

func (mstore *MongoDBStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save adds a single session to the response and persist session in mongoDB collection

If the Options.MaxAge of the session is <= 0 then the session file will be deleted from the store path. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.

type MongoDBStoreConfig

type MongoDBStoreConfig struct {

	// whether to create TTL index(https://docs.mongodb.com/manual/core/index-ttl/)
	// for the session document
	IndexTTL bool

	// gorilla-sessions options
	SessionOptions sessions.Options
}

MongoDBStoreConfig is a configuration options for MongoDBStore

Jump to

Keyboard shortcuts

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