Feat: redis for session storing
This commit is contained in:
parent
7d5016ae40
commit
b0e9c38732
3 changed files with 31 additions and 4 deletions
|
@ -10,6 +10,11 @@ Host = 127.0.0.1:3306
|
||||||
Name = v3
|
Name = v3
|
||||||
TablePrefix = v3_
|
TablePrefix = v3_
|
||||||
|
|
||||||
|
[Redis]
|
||||||
|
Server = 127.0.0.1:6379
|
||||||
|
Password = 52121225
|
||||||
|
DB = 0
|
||||||
|
|
||||||
[Captcha]
|
[Captcha]
|
||||||
Height = 60
|
Height = 60
|
||||||
Width = 200
|
Width = 200
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/HFO4/cloudreve/pkg/conf"
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-contrib/sessions/memstore"
|
"github.com/gin-contrib/sessions/memstore"
|
||||||
|
@ -9,14 +10,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Store session存储
|
// Store session存储
|
||||||
var Store memstore.Store
|
var Store redis.Store
|
||||||
|
|
||||||
// Session 初始化session
|
// Session 初始化session
|
||||||
func Session(secret string) gin.HandlerFunc {
|
func Session(secret string) gin.HandlerFunc {
|
||||||
Store, err := redis.NewStore(10, "tcp", "127.0.0.1:6379", "52121225", []byte("secret"))
|
if conf.RedisConfig.Server != "" {
|
||||||
|
// 如果配置使用了Redis
|
||||||
|
var err error
|
||||||
|
Store, err = redis.NewStoreWithDB(10, "tcp", conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, []byte(secret))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Panic("无法连接到Redis:%s", err)
|
util.Log().Panic("无法连接到Redis:%s", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Store = memstore.NewStore([]byte(secret))
|
||||||
|
}
|
||||||
// Also set Secure: true if using SSL, you should though
|
// Also set Secure: true if using SSL, you should though
|
||||||
Store.Options(sessions.Options{HttpOnly: true, MaxAge: 7 * 86400, Path: "/"})
|
Store.Options(sessions.Options{HttpOnly: true, MaxAge: 7 * 86400, Path: "/"})
|
||||||
return sessions.Sessions("cloudreve-session", Store)
|
return sessions.Sessions("cloudreve-session", Store)
|
||||||
|
|
|
@ -38,6 +38,20 @@ type captcha struct {
|
||||||
CaptchaLen int `validate:"gt=0"`
|
CaptchaLen int `validate:"gt=0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// redis 配置
|
||||||
|
type redis struct {
|
||||||
|
Server string
|
||||||
|
Password string
|
||||||
|
DB string
|
||||||
|
}
|
||||||
|
|
||||||
|
// RedisConfig Redis服务器配置
|
||||||
|
var RedisConfig = &redis{
|
||||||
|
Server: "",
|
||||||
|
Password: "",
|
||||||
|
DB: "0",
|
||||||
|
}
|
||||||
|
|
||||||
// DatabaseConfig 数据库配置
|
// DatabaseConfig 数据库配置
|
||||||
var DatabaseConfig = &database{
|
var DatabaseConfig = &database{
|
||||||
Type: "UNSET",
|
Type: "UNSET",
|
||||||
|
@ -79,6 +93,7 @@ func Init(path string) {
|
||||||
"Database": DatabaseConfig,
|
"Database": DatabaseConfig,
|
||||||
"System": SystemConfig,
|
"System": SystemConfig,
|
||||||
"Captcha": CaptchaConfig,
|
"Captcha": CaptchaConfig,
|
||||||
|
"Redis": RedisConfig,
|
||||||
}
|
}
|
||||||
for sectionName, sectionStruct := range sections {
|
for sectionName, sectionStruct := range sections {
|
||||||
err = mapSection(sectionName, sectionStruct)
|
err = mapSection(sectionName, sectionStruct)
|
||||||
|
|
Loading…
Add table
Reference in a new issue