Files
eiblog/pkg/mid/session.go
2021-04-26 15:51:16 +08:00

35 lines
699 B
Go

// Package mid provides ...
package mid
import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
// SessionOpts 设置选项
type SessionOpts struct {
Name string
Secure bool // required
Secret []byte // required
// redis store
RedisAddr string
RedisPwd string
}
// SessionMiddleware session中间件
func SessionMiddleware(opts SessionOpts) gin.HandlerFunc {
store := cookie.NewStore(opts.Secret)
store.Options(sessions.Options{
MaxAge: 86400 * 30,
Path: "/",
Secure: opts.Secure,
HttpOnly: true,
})
name := "SESSIONID"
if opts.Name != "" {
name = opts.Name
}
return sessions.Sessions(name, store)
}