https://github.com/bh90210/vagorillasessionsstores
Various back-end stores for Gorilla Sessions (Badger, Mongo, Dgraph)
https://github.com/bh90210/vagorillasessionsstores
badgerdb dgraph go golang gorilla-sessions mongodb store
Last synced: about 1 month ago
JSON representation
Various back-end stores for Gorilla Sessions (Badger, Mongo, Dgraph)
- Host: GitHub
- URL: https://github.com/bh90210/vagorillasessionsstores
- Owner: bh90210
- License: unlicense
- Created: 2020-05-04T23:51:24.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-12-18T22:51:42.000Z (over 5 years ago)
- Last Synced: 2026-01-15T07:28:33.143Z (5 months ago)
- Topics: badgerdb, dgraph, go, golang, gorilla-sessions, mongodb, store
- Language: Go
- Homepage:
- Size: 81.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vagorillasessionsstores [](https://godoc.org/github.com/bh90210/va-gorilla-sessions-store)
Collection of various Gorilla Sessions back-end stores.
# Install
```bash
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:
```go
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](https://dgraph.io/docs/badger) for more):
```go
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):
```go
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:
```yaml
sessionid: string @index(hash) .
sessionvalue: string .
type Session {
sessionid
sessionvalue
}
```
```go
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:
```go
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")))
```