-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
36 lines (32 loc) · 1017 Bytes
/
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
// Session implements middleware for easily using github.com/gorilla/sessions
// within echo. This package was originally inspired from the
// https://github.com/ipfans/echo-session package, and modified to provide more
// functionality
package session
import (
"github.com/webx-top/echo"
X "github.com/webx-top/webx"
ss "github.com/webx-top/webx/lib/session"
"github.com/webx-top/webx/lib/session/ssi"
)
type Sessionser interface {
InitSession(ssi.Session)
}
func Sessions(name string, store ss.Store) echo.MiddlewareFunc {
return echo.MiddlewareFunc(func(h echo.Handler) echo.Handler {
return echo.HandlerFunc(func(ctx echo.Context) error {
c := X.X(ctx)
s := ss.NewMySession(store, name, ctx)
if se, ok := interface{}(c).(Sessionser); ok {
se.InitSession(s)
}
err := h.Handle(c)
s.Save()
return err
})
})
}
func Middleware(options *ssi.Options, setting interface{}) echo.MiddlewareFunc {
store := ss.StoreEngine(options, setting)
return Sessions(ssi.DefaultName, store)
}