-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
72 lines (61 loc) · 1.81 KB
/
options.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
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
package dynamodbstore
import (
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
)
// StoreOptions used to configure the dynamodb session store
type StoreOptions struct {
tableName string
sessionOptions *sessions.Options
keyPairs [][]byte
defaultMaxAge int
}
// Option used to configure the store
type Option func(opts *StoreOptions)
// TableName update the table name
func TableName(name string) Option {
return func(opts *StoreOptions) {
opts.tableName = name
}
}
// SessionOptions update the session options provided to github.com/gorilla/sessions
func SessionOptions(sessionOpts *sessions.Options) Option {
return func(opts *StoreOptions) {
opts.sessionOptions = sessionOpts
}
}
// KeyPairs update the key pairs provided to github.com/gorilla/sessions
func KeyPairs(keyPairs ...[]byte) Option {
return func(opts *StoreOptions) {
opts.keyPairs = keyPairs
}
}
// DefaultMaxAge update the default max age provided to github.com/gorilla/sessions
func DefaultMaxAge(age int) Option {
return func(opts *StoreOptions) {
opts.defaultMaxAge = age
}
}
// NewDynamodbStoreWithOptions new dynamodb session store using options
func NewDynamodbStoreWithOptions(ddb *dynamodb.DynamoDB, options ...Option) *DynamodbStore {
// setup a "default" store using the original values
opts := &StoreOptions{
tableName: DefaultTableName,
sessionOptions: &sessions.Options{
Path: "/",
MaxAge: sessionExpire,
},
defaultMaxAge: 60 * 20, // 20 minutes seems like a reasonable default
}
for _, opt := range options {
opt(opts)
}
return &DynamodbStore{
DB: ddb,
TableName: opts.tableName,
Codecs: securecookie.CodecsFromPairs(opts.keyPairs...),
Options: opts.sessionOptions,
DefaultMaxAge: opts.defaultMaxAge,
}
}