2017-03-18 22:43:04 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-19 11:55:48 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2017-08-08 15:00:34 +02:00
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/sessions"
|
|
|
|
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
2020-05-07 06:34:17 +02:00
|
|
|
|
|
|
|
"github.com/kataras/iris/v12/_examples/sessions/overview/example"
|
2017-03-18 22:43:04 +01:00
|
|
|
)
|
|
|
|
|
2021-02-19 07:05:13 +01:00
|
|
|
// 1. Install Redis:
|
|
|
|
// 1.1 Windows: https://github.com/ServiceStack/redis-windows
|
|
|
|
// 1.2 Other: https://redis.io/download
|
2021-02-19 07:49:15 +01:00
|
|
|
// 2. Run command: go run -mod=mod main.go
|
2021-02-19 07:05:13 +01:00
|
|
|
//
|
|
|
|
// Tested with redis version 3.0.503.
|
2017-03-18 22:43:04 +01:00
|
|
|
func main() {
|
2019-08-09 07:24:58 +02:00
|
|
|
// These are the default values,
|
|
|
|
// you can replace them based on your running redis' server settings:
|
2019-06-22 13:46:20 +02:00
|
|
|
db := redis.New(redis.Config{
|
|
|
|
Network: "tcp",
|
2020-07-19 11:55:48 +02:00
|
|
|
Addr: getenv("REDIS_ADDR", "127.0.0.1:6379"),
|
2019-06-22 13:46:20 +02:00
|
|
|
Timeout: time.Duration(30) * time.Second,
|
|
|
|
MaxActive: 10,
|
2020-10-04 15:50:21 +02:00
|
|
|
Username: "",
|
2019-06-22 13:46:20 +02:00
|
|
|
Password: "",
|
|
|
|
Database: "",
|
2020-10-10 12:18:33 +02:00
|
|
|
Prefix: "myapp-",
|
2020-10-12 01:07:04 +02:00
|
|
|
Driver: redis.GoRedis(), // defaults.
|
2019-08-06 17:40:13 +02:00
|
|
|
})
|
|
|
|
|
2019-08-09 07:24:58 +02:00
|
|
|
// Optionally configure the underline driver:
|
2020-10-04 15:50:21 +02:00
|
|
|
// driver := redis.GoRedis()
|
|
|
|
// driver.ClientOptions = redis.Options{...}
|
|
|
|
// driver.ClusterOptions = redis.ClusterOptions{...}
|
|
|
|
// redis.New(redis.Config{Driver: driver, ...})
|
2017-03-18 22:43:04 +01:00
|
|
|
|
2018-04-22 12:52:36 +02:00
|
|
|
defer db.Close() // close the database connection if application errored.
|
|
|
|
|
|
|
|
sess := sessions.New(sessions.Config{
|
2020-05-07 06:34:17 +02:00
|
|
|
Cookie: "_session_id",
|
2020-04-21 08:27:28 +02:00
|
|
|
Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute,
|
|
|
|
AllowReclaim: true,
|
|
|
|
CookieSecureTLS: true,
|
2019-06-22 13:46:20 +02:00
|
|
|
})
|
2017-03-18 22:43:04 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// IMPORTANT:
|
|
|
|
//
|
2017-07-10 17:32:42 +02:00
|
|
|
sess.UseDatabase(db)
|
2017-03-18 22:43:04 +01:00
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
app := example.NewApp(sess)
|
2020-07-19 11:55:48 +02:00
|
|
|
|
|
|
|
// 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
|
2017-03-18 22:43:04 +01:00
|
|
|
}
|