-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession.go
68 lines (59 loc) · 1.45 KB
/
session.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
package january
import (
"database/sql"
"github.com/alexedwards/scs/mysqlstore"
"github.com/alexedwards/scs/postgresstore"
"github.com/alexedwards/scs/redisstore"
"github.com/alexedwards/scs/v2"
"github.com/gomodule/redigo/redis"
"net/http"
"strconv"
"strings"
"time"
)
type Session struct {
CookieLifetime string
CookiePersist string
CookieName string
CookieDomain string
SessionType string
CookieSecure string
DBPool *sql.DB
RedisPool *redis.Pool
}
func (s *Session) InitSession() *scs.SessionManager {
var persist = false
var secure = false
// session duration
minutes, err := strconv.Atoi(s.CookieLifetime)
if err != nil {
minutes = 60
}
// should cookie persist?
if strings.ToLower(s.CookiePersist) == "true" {
persist = true
}
// should cookie be secure?
if strings.ToLower(s.CookieSecure) == "true" {
secure = true
}
// create session
session := scs.New()
session.Lifetime = time.Duration(minutes) * time.Minute
session.Cookie.Persist = persist
session.Cookie.Name = s.CookieName
session.Cookie.Secure = secure
session.Cookie.Domain = s.CookieDomain
session.Cookie.SameSite = http.SameSiteLaxMode
// select session store
switch strings.ToLower(s.SessionType) {
case "redis":
session.Store = redisstore.New(s.RedisPool)
case "mysql", "mariadb":
session.Store = mysqlstore.New(s.DBPool)
case "postgres", "postgresql":
session.Store = postgresstore.New(s.DBPool)
default:
}
return session
}