iris/_examples/sessions/database/redis/main.go
Gerasimos (Makis) Maropoulos 302597faac example: sessions: scale out using redis
Former-commit-id: a811648e0bdf83c289656426f8ba67d785b7d5cc
2020-07-19 12:55:48 +03:00

78 lines
1.8 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/sessions"
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
"github.com/kataras/iris/v12/_examples/sessions/overview/example"
)
// tested with redis version 3.0.503.
// for windows see: https://github.com/ServiceStack/redis-windows
func main() {
// These are the default values,
// you can replace them based on your running redis' server settings:
db := redis.New(redis.Config{
Network: "tcp",
Addr: getenv("REDIS_ADDR", "127.0.0.1:6379"),
Timeout: time.Duration(30) * time.Second,
MaxActive: 10,
Password: "",
Database: "",
Prefix: "",
Delim: "-",
Driver: redis.Redigo(), // redis.Radix() can be used instead.
})
// Optionally configure the underline driver:
// driver := redis.Redigo()
// driver.MaxIdle = ...
// driver.IdleTimeout = ...
// driver.Wait = ...
// redis.Config {Driver: driver}
// Close connection when control+C/cmd+C
iris.RegisterOnInterrupt(func() {
db.Close()
})
defer db.Close() // close the database connection if application errored.
sess := 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,
})
//
// IMPORTANT:
//
sess.UseDatabase(db)
app := example.NewApp(sess)
// TIP scaling-out Iris sessions using redis:
// $ docker-compose up
// http://localhost:8080/set/$key/$value
// The value will be available on all Iris servers as well.
// E.g. http://localhost:9090/get/$key and vice versa.
addr := fmt.Sprintf(":%s", getenv("PORT", "8080"))
app.Listen(addr)
}
func getenv(key string, def string) string {
if v := os.Getenv(strings.ToUpper(key)); v != "" {
return v
}
return def
}