-
Notifications
You must be signed in to change notification settings - Fork 12
/
session.go
109 lines (95 loc) · 2.51 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ginsession
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-session/session"
)
type (
// ErrorHandleFunc error handling function
ErrorHandleFunc func(*gin.Context, error)
// Config defines the config for Session middleware
Config struct {
// error handling when starting the session
ErrorHandleFunc ErrorHandleFunc
// keys stored in the context
StoreKey string
// keys stored in the context
ManageKey string
// defines a function to skip middleware.Returning true skips processing
// the middleware.
Skipper func(*gin.Context) bool
}
)
var (
storeKey string
manageKey string
// DefaultConfig is the default Recover middleware config.
DefaultConfig = Config{
ErrorHandleFunc: func(ctx *gin.Context, err error) {
ctx.AbortWithError(500, err)
},
StoreKey: "github.com/go-session/gin-session/store",
ManageKey: "github.com/go-session/gin-session/manage",
Skipper: func(_ *gin.Context) bool {
return false
},
}
)
// New create a session middleware
func New(opt ...session.Option) gin.HandlerFunc {
return NewWithConfig(DefaultConfig, opt...)
}
// NewWithConfig create a session middleware
func NewWithConfig(config Config, opt ...session.Option) gin.HandlerFunc {
if config.ErrorHandleFunc == nil {
config.ErrorHandleFunc = DefaultConfig.ErrorHandleFunc
}
manageKey = config.ManageKey
if manageKey == "" {
manageKey = DefaultConfig.ManageKey
}
storeKey = config.StoreKey
if storeKey == "" {
storeKey = DefaultConfig.StoreKey
}
manage := session.NewManager(opt...)
return func(ctx *gin.Context) {
if config.Skipper != nil && config.Skipper(ctx) {
ctx.Next()
return
}
ctx.Set(manageKey, manage)
store, err := manage.Start(context.Background(), ctx.Writer, ctx.Request)
if err != nil {
config.ErrorHandleFunc(ctx, err)
return
}
ctx.Set(storeKey, store)
ctx.Next()
}
}
// FromContext Get session storage from context
func FromContext(ctx *gin.Context) session.Store {
v, ok := ctx.Get(storeKey)
if ok {
return v.(session.Store)
}
return nil
}
// Destroy a session
func Destroy(ctx *gin.Context) error {
v, ok := ctx.Get(manageKey)
if !ok {
return fmt.Errorf("invalid session manager")
}
return v.(*session.Manager).Destroy(nil, ctx.Writer, ctx.Request)
}
// Refresh a session and return to session storage
func Refresh(ctx *gin.Context) (session.Store, error) {
v, ok := ctx.Get(manageKey)
if !ok {
return nil, fmt.Errorf("invalid session manager")
}
return v.(*session.Manager).Refresh(nil, ctx.Writer, ctx.Request)
}