forked from kimiazhu/ginweb-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
36 lines (31 loc) · 964 Bytes
/
mongo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Description: a session middleware based on mongodb, gorilla-session and gin-gonic
// Author: ZHU HAIHUA
// Since: 2016-04-07 20:20
package sessions
import (
ginsession "github.com/gin-gonic/contrib/sessions"
"github.com/gorilla/sessions"
"github.com/kidstuff/mongostore"
"gopkg.in/mgo.v2"
)
type MongoStore interface {
ginsession.Store
}
type mongoStore struct {
*mongostore.MongoStore
}
// NewMongoStore return a session store based on mongodb
//
func NewMongoStore(s *mgo.Session, dbName, collectionName string, maxAge int, ensureTTL bool, keyPairs ...[]byte) MongoStore {
store := mongostore.NewMongoStore(s.DB(dbName).C(collectionName), maxAge, ensureTTL, keyPairs...)
return &mongoStore{store}
}
func (c *mongoStore) Options(options ginsession.Options) {
c.MongoStore.Options = &sessions.Options{
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HttpOnly,
}
}