Skip to content

Various back-end stores for Gorilla Sessions (Badger, Mongo, Dgraph)

License

Notifications You must be signed in to change notification settings

bh90210/vagorillasessionsstores

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vagorillasessionsstores GoDoc

Collection of various Gorilla Sessions back-end stores.

Install

go get github.com/bh90210/vaGorillaSessionsStores

Use

Errors are excluded for brevity.

Badger

note: Badger will not work in distributed environments. Use it for local testing or single server scenarios.

Using the store is very simple:

import stores "github.com/bh90210/vagorillasessionsstores"

store, _ := stores.NewBadgerStore("/path/to/data", []byte(os.Getenv("SESSION_KEY")))

If path is empty data will be stored in system's tmp directory.

Start a store with custom options (see Badger's docs for more):

import stores "github.com/bh90210/vagorillasessionsstores"

opts := badger.Options{
		Dir: "/data/dir",
}
store, _ := stores.NewBadgerStoreWithOpts(opts,[]byte(os.Getenv("SESSION_KEY")))

Help functions

Two helper functions for direct back-end session manipulation without http request.

Mongo

Starting a store entails passing credentials and client options (official go mongo driver is necessary):

import (
	stores "github.com/bh90210/vagorillasessionsstores"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var cred options.Credential

cred.AuthSource = "YourAuthSource"
cred.Username = "YourUserName"
cred.Password = "YourPassword"

clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI")).SetAuth(cred)

ctx := context.Background()
client, _ := mongo.Connect(ctx, opts)

store, _ := stores.NewMongoStore(client, "databaseName", "collectionName", []byte(os.Getenv("SESSION_KEY")))

If 'databaseName' & 'collectionName' are left empty the defaults are used ('sessions' & 'store').

Dgraph

store uses dgo/v200

Assumed schema:

sessionid: string @index(hash) .
sessionvalue: string . 
type Session {
  sessionid
  sessionvalue
}
import (
	stores "github.com/bh90210/vagorillasessionsstores"
	"google.golang.org/grpc"
)

conn, _err_ := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())

store, _ := stores.NewDgraphStore(conn, []byte(os.Getenv("SESSION_KEY")))

You can also let schema initiation to the store:

import (
	stores "github.com/bh90210/vagorillasessionsstores"
	"google.golang.org/grpc"
)

conn, _err_ := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())

store, _ := stores.NewDgraphStoreWithSchema(conn, []byte(os.Getenv("SESSION_KEY")))