-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcouchbasestore.go
More file actions
196 lines (178 loc) · 4.69 KB
/
Copy pathcouchbasestore.go
File metadata and controls
196 lines (178 loc) · 4.69 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//Copyright 2013 Srinath. All rights Reserved.
//This Software is licensed under MIT license available in the LICENSE file.
package couchbasestore
import (
"encoding/base32"
"errors"
"github.com/couchbaselabs/go-couchbase"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"net/http"
"strings"
)
//CouchStore Definition
type CouchStore struct {
endpoint string
bucket *couchbase.Bucket
codecs []securecookie.Codec
options *sessions.Options
pool *couchbase.Pool
bucketName string
}
//No. Of Retries. The program that uses this package can change this.
var Retries = 5
//MaxAge Error.
var ErrMaxAge = errors.New("max age should be greater than zero")
//NewCouchStore creates a new CouchStore.
func NewCouchStore(endpoint string, pool string, bucket string, path string, maxAge int, keyPairs ...[]byte) (*CouchStore, error) {
c, err := couchbase.Connect(endpoint)
if err != nil {
return nil, err
}
p, err := c.GetPool(pool)
if err != nil {
return nil, err
}
if path == "" {
path = "/"
}
if maxAge <= 0 {
return nil, ErrMaxAge
}
return &CouchStore{
endpoint: endpoint,
bucket: nil,
codecs: securecookie.CodecsFromPairs(keyPairs...),
options: &sessions.Options{
Path: path,
MaxAge: maxAge,
},
pool: &p,
bucketName: bucket,
}, nil
}
//Internal Function to get a bucket from a pool
func (c *CouchStore) getBucket() *couchbase.Bucket {
if c.bucket == nil {
c.bucket, _ = c.pool.GetBucket(c.bucketName)
}
return c.bucket
}
//Internal Function to close couchbase bucket
func (c *CouchStore) closeBucket() {
c.bucket.Close()
}
//Close CouchStore
func (c *CouchStore) Close() {
c.closeBucket()
}
//Get Session data from CouchStore. name is the key in the cookie against which the cookie string is set.
func (c *CouchStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(c, name)
}
//New creates a new session. CouchStore.Get will perform this in case there is no existing session.
func (c *CouchStore) New(r *http.Request, name string) (*sessions.Session, error) {
session := sessions.NewSession(c, name)
//default values
session.Options = &sessions.Options{
Path: c.options.Path,
MaxAge: c.options.MaxAge,
}
session.IsNew = true
var err error
if cook, errCookie := r.Cookie(name); errCookie == nil {
err = securecookie.DecodeMulti(name, cook.Value, &session.ID, c.codecs...)
if err == nil {
err = c.load(session)
if err == nil {
session.IsNew = false
}
}
}
return session, err
}
//Save Session
func (c *CouchStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
// Build an alphanumeric key for the couchbase store.
if session.ID == "" {
session.ID = strings.TrimRight(base32.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(32)), "=")
}
if err := c.save(session); err != nil {
return err
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID, c.codecs...)
if err != nil {
return err
}
http.SetCookie(w, sessions.NewCookie(session.Name(), encoded, session.Options))
return nil
}
//Retry
func mayBeRetry(f func() error) error {
var tries = 0
var err error
for tries < Retries {
err = f()
if err == couchbase.TimeoutError || err == couchbase.ErrTimeout {
tries++
} else if err == nil {
return nil
} else {
return err
}
}
return err
}
//Delete session from CouchStore and HTTP cookie
func (c *CouchStore) Delete(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
c.getBucket()
_ = mayBeRetry(func() error {
return c.bucket.Delete(session.ID)
})
//defer c.closeBucket()
// Set cookie to expire.
options := *session.Options
options.MaxAge = -1
http.SetCookie(w, sessions.NewCookie(session.Name(), "", &options))
// Clear session values.
for k := range session.Values {
delete(session.Values, k)
}
return nil
}
//Save session to Couchbase
func (c *CouchStore) save(session *sessions.Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(), session.Values, c.codecs...)
if err != nil {
return err
}
c.getBucket()
//defer c.closeBucket()
errSet := mayBeRetry(func() error {
return c.bucket.Set(session.ID, session.Options.MaxAge, encoded)
})
if errSet != nil {
return errSet
}
return nil
}
//Load Session from couchbase
func (c *CouchStore) load(session *sessions.Session) error {
c.getBucket()
//defer c.closeBucket()
var data interface{}
err := mayBeRetry(func() error {
return c.bucket.Get(session.ID, &data)
})
if err != nil {
return nil
}
if data == nil {
return nil
}
str := data.(string)
if err = securecookie.DecodeMulti(session.Name(), str, &session.Values, c.codecs...); err != nil {
return err
}
return nil
}