-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
70 lines (59 loc) · 1.44 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
69
70
package session
import (
"database/sql"
"net/http"
"strconv"
"strings"
"time"
"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"
)
type Session struct {
CookieLifetime string
CookiePersist string
CookieName string
CookieDomain string
SessionType string
CookieSecure string
DBPool *sql.DB
RedisPool *redis.Pool
}
func (f *Session) InitSession() *scs.SessionManager {
var persist, secure bool
// how long do we keep the session?
min, err := strconv.Atoi(f.CookieLifetime)
if err != nil {
min = 60
}
// cookies persist?
if strings.ToLower(f.CookiePersist) == "true" {
persist = true
}
// cookies secure?
if strings.ToLower(f.CookieSecure) == "true" {
secure = true
}
// create session
session := scs.New()
session.Lifetime = time.Duration(min) * time.Minute
session.Cookie.Persist = persist
session.Cookie.Name = f.CookieName
session.Cookie.Secure = secure
session.Cookie.Domain = f.CookieDomain
session.Cookie.SameSite = http.SameSiteLaxMode
// which session store?
switch strings.ToLower(f.SessionType) {
case "redis":
session.Store = redisstore.New(f.RedisPool)
case "mysql", "mariadb":
session.Store = mysqlstore.New(f.DBPool)
case "postgres", "postgresql":
session.Store = postgresstore.New(f.DBPool)
default:
// cookie
}
return session
}