-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (66 loc) · 1.7 KB
/
config.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
73
74
package nssession
import (
"errors"
"net/http"
"time"
"github.com/no-src/nssession/store"
)
var (
defaultConfig *Config
errNilConfig = errors.New("config is nil")
errNilStore = errors.New("store is nil")
// DefaultCookieName the default cookie name for session
DefaultCookieName = "ns-session-id"
// DefaultSessionPrefix the default session prefix for session store
DefaultSessionPrefix = "session"
)
// Config the configuration of session
type Config struct {
// Connection the connection string of the session
Connection string
// Expiration the expiration time of the session
Expiration time.Duration
// SessionPrefix the session prefix for session store
SessionPrefix string
// Store the session store component
Store store.Store
// Cookie the settings of the cookie
Cookie Cookie
}
// Cookie the settings of the cookie
type Cookie struct {
// Name the cookie name for session
Name string
// Path set the Path property of the cookie
Path string
// Domain set the Domain property of the cookie
Domain string
// Expires set the Expires property of the cookie
Expires time.Time
// MaxAge set the Max-Age property of the cookie
MaxAge int
// Secure set the Secure property of the cookie
Secure bool
// SameSite set the SameSite property of the cookie
SameSite http.SameSite
}
// InitDefaultConfig initial the default global session config
func InitDefaultConfig(c *Config) error {
if c == nil {
return errNilConfig
}
if c.Store == nil {
return errNilStore
}
if len(c.Cookie.Name) == 0 {
c.Cookie.Name = DefaultCookieName
}
if len(c.Cookie.Path) == 0 {
c.Cookie.Path = "/"
}
if len(c.SessionPrefix) == 0 {
c.SessionPrefix = DefaultSessionPrefix
}
defaultConfig = c
return nil
}