mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-13 01:42:27 +08:00
refactor: eiblog
This commit is contained in:
34
pkg/mid/session.go
Normal file
34
pkg/mid/session.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user