-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
69 lines (63 loc) · 2.19 KB
/
common.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
package gorillaradix
import (
"github.com/gorilla/sessions"
"github.com/mediocregopher/radix/v3"
"time"
)
const (
defaultMaxAge = 86400 // how long should the session persist for
defaultMaxLength = 1024 * 32 // 32KB of session storage
defaultKeyPrefix = "session_" // start session names with "session_"
defaultCookiePath = "/" // have it be site wide
defaultSecret = "notAGreatSecret" // default secret for session validation
)
type SessionOptions struct {
sessions.Options
MaxLength int // Max amount of storage to be allowed in session data for a given user
KeyPrefix string // within redis, what should session data keys be prefixed with
Secret string // Secret is for validating a session key
}
// Universal Redis Options
type ConnectionOptions struct {
Password string // Password for the redis server
PingTimeout time.Duration // How often should we ping the redis instance
Timeout time.Duration // Timeout on connecting
}
// helper function for creating the connection function for radix
// both cluster and regular redis will need this function
func createRadixConnectionFunction(configuration ConnectionOptions) (connectFunction radix.ConnFunc) {
// If there is a password specified, we need to add it as part of the connection function
if configuration.Password != "" {
connectFunction = func(network, addr string) (radix.Conn, error) {
return radix.Dial(network, addr,
radix.DialTimeout(configuration.Timeout),
radix.DialAuthPass(configuration.Password),
)
}
} else {
connectFunction = func(network, addr string) (radix.Conn, error) {
return radix.Dial(network, addr,
radix.DialTimeout(configuration.Timeout),
)
}
}
return
}
// Apply reasonable defaults to the options
func applyDefaultSessionConfiguration(options *SessionOptions) {
if options.MaxLength == 0 {
options.MaxLength = defaultMaxLength
}
if options.KeyPrefix == "" {
options.KeyPrefix = defaultKeyPrefix
}
if options.Options.Path == "" {
options.Options.Path = defaultCookiePath
}
if options.Options.MaxAge == 0 {
options.Options.MaxAge = defaultMaxAge
}
if options.Secret == "" {
options.Secret = defaultSecret
}
}