Create redis session engine
This commit is contained in:
38
app/main.go
38
app/main.go
@ -1,16 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"iris-test/app/cfg"
|
||||
"iris-test/app/db"
|
||||
"iris-test/app/logging"
|
||||
"iris-test/app/views"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
||||
)
|
||||
|
||||
var redisDB *redis.Database
|
||||
|
||||
func createSessionEngine() *sessions.Sessions {
|
||||
redisAddr := fmt.Sprintf("%s:%d", cfg.Config.Redis.Host, cfg.Config.Redis.Port)
|
||||
|
||||
redisDB = redis.New(redis.Config{
|
||||
Network: "tcp",
|
||||
Addr: redisAddr,
|
||||
Timeout: time.Duration(30) * time.Second,
|
||||
MaxActive: 10,
|
||||
Username: cfg.Config.Redis.Username,
|
||||
Password: cfg.Config.Redis.Password,
|
||||
Database: cfg.Config.Redis.Database,
|
||||
Prefix: cfg.Config.Redis.Prefix,
|
||||
Driver: redis.GoRedis(), // defaults to this driver.
|
||||
// To set a custom, existing go-redis client, use the "SetClient" method:
|
||||
// Driver: redis.GoRedis().SetClient(customGoRedisClient)
|
||||
})
|
||||
|
||||
sessions_engine := sessions.New(sessions.Config{
|
||||
Cookie: "_session_id",
|
||||
Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute,
|
||||
AllowReclaim: true,
|
||||
CookieSecureTLS: true,
|
||||
})
|
||||
|
||||
sessions_engine.UseDatabase(redisDB)
|
||||
|
||||
return sessions_engine
|
||||
}
|
||||
|
||||
func createApp() *iris.Application {
|
||||
sessions_engine := createSessionEngine()
|
||||
app := iris.New()
|
||||
app.Use(sessions_engine.Handler())
|
||||
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
||||
views.CreateRouter(app)
|
||||
return app
|
||||
@ -22,5 +59,6 @@ func main() {
|
||||
db.InitDB()
|
||||
|
||||
app := createApp()
|
||||
defer redisDB.Close()
|
||||
app.Listen(":8000")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user