diff --git a/_examples/sessions/database/redis/Dockerfile b/_examples/sessions/database/redis/Dockerfile new file mode 100644 index 00000000..a1d50355 --- /dev/null +++ b/_examples/sessions/database/redis/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:latest AS builder +RUN apt-get update +WORKDIR /go/src/app +ENV GO111MODULE=on \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 +# Caching go modules and build the binary. +COPY go.mod . +RUN go mod download +COPY . . +RUN go install + +FROM scratch +COPY --from=builder /go/bin/app . +ENTRYPOINT ["./app"] \ No newline at end of file diff --git a/_examples/sessions/database/redis/docker-compose.yml b/_examples/sessions/database/redis/docker-compose.yml new file mode 100644 index 00000000..324b7534 --- /dev/null +++ b/_examples/sessions/database/redis/docker-compose.yml @@ -0,0 +1,24 @@ +# docker-compose up [--build] +version: '3' + +services: + redis-server: + image: redis + app1: + build: . + depends_on: + - redis-server + ports: + - 8080:8080 + environment: + - PORT=8080 + - REDIS_ADDR=redis-server:6379 + app2: + build: . + depends_on: + - redis-server + ports: + - 9090:9090 + environment: + - PORT=9090 + - REDIS_ADDR=redis-server:6379 \ No newline at end of file diff --git a/_examples/sessions/database/redis/go.mod b/_examples/sessions/database/redis/go.mod new file mode 100644 index 00000000..81e02483 --- /dev/null +++ b/_examples/sessions/database/redis/go.mod @@ -0,0 +1,6 @@ +module app + +go 1.14 + +require github.com/kataras/iris/v12 v12.1.9-0.20200719040914-c21125c094d6 + diff --git a/_examples/sessions/database/redis/main.go b/_examples/sessions/database/redis/main.go index 9891b67a..f8333dc7 100644 --- a/_examples/sessions/database/redis/main.go +++ b/_examples/sessions/database/redis/main.go @@ -1,6 +1,9 @@ package main import ( + "fmt" + "os" + "strings" "time" "github.com/kataras/iris/v12" @@ -18,7 +21,7 @@ func main() { // you can replace them based on your running redis' server settings: db := redis.New(redis.Config{ Network: "tcp", - Addr: "127.0.0.1:6379", + Addr: getenv("REDIS_ADDR", "127.0.0.1:6379"), Timeout: time.Duration(30) * time.Second, MaxActive: 10, Password: "", @@ -55,5 +58,20 @@ func main() { sess.UseDatabase(db) app := example.NewApp(sess) - app.Listen(":8080") + + // 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 }