Skip to content

Instantly share code, notes, and snippets.

@jsonw23
Last active April 8, 2022 15:47
Show Gist options
  • Save jsonw23/1f2cfeb573e50b38e8f79d28a949af9b to your computer and use it in GitHub Desktop.
Save jsonw23/1f2cfeb573e50b38e8f79d28a949af9b to your computer and use it in GitHub Desktop.
package middleware
import (
"fmt"
"log"
"os"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
)
func InitAuthStore(r *gin.Engine) {
serviceDisc, deployed := os.LookupEnv("COPILOT_SERVICE_DISCOVERY_ENDPOINT")
var redisHost string
if deployed {
redisHost = fmt.Sprintf("redis.%s:6379", serviceDisc)
} else {
redisHost = "localhost:6379"
}
store, error := redis.NewStore(10, "tcp",
redisHost, "",
[]byte(os.Getenv("AUTH_CLIENT_SECRET")))
if error != nil {
log.Panic(error)
}
r.Use(sessions.Sessions("auth", store))
}
func Auth() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
session.Set("test", "value")
session.Save()
c.Next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment