func Sessions() gin.HandlerFunc { switch conf.SESSION_STORE { case conf.REDIS: store, err := gin_sessions.NewRedisStore(10, "tcp", conf.REDIS_SERVER, conf.REDIS_PWD, []byte("secret")) if err != nil { panic(err) } return gin_sessions.Sessions("mysession", store) default: store := gin_sessions.NewCookieStore([]byte("secret")) return gin_sessions.Sessions("mysession", store) } }
func NewRedisSession(engine *gin.Engine, conf AuthConf) { store, err := sessions.NewRedisStore(conf.Redis.Size, conf.Redis.Network, conf.Redis.Address, conf.Redis.Password, []byte(conf.Session.Secret)) if err != nil { panic(err) } newSession(engine, conf, store) }
func main() { r := gin.Default() r.Use(RequestIdMiddleware()) DB, err := db.Connect(db.ConnectOpts{ Address: fmt.Sprintf("%s:%d", os.Getenv("RETHINKDB_PORT_28015_TCP_ADDR"), 28015), Database: "test", }) if err != nil { log.Fatalln(err.Error()) } db.TableCreate("users").RunWrite(DB) db.TableCreate("resources").RunWrite(DB) store, err := sessions.NewRedisStore(10, "tcp", fmt.Sprintf("%s:%d", os.Getenv("REDIS_1_PORT_6379_TCP_ADDR"), 6379), "", []byte(os.Getenv("REDIS_SECRET"))) if err != nil { log.Fatalln(err.Error()) } gob.Register(models.User{}) r.Use(sessions.Sessions("session", store)) api.Init(r, DB, RequestAuthMiddleware) login.Init(r, DB) r.Any("/ping", func(c *gin.Context) { if DB.IsConnected() { c.String(200, "ok") } else { c.String(500, "not ok") } }) r.Run(":8080") // listen and serve on 0.0.0.0:8080 }
// setupMiddleware is an internal method where we setup GIN middleware func setupMiddleware(r *gin.Engine) { // TODO: CACHE_URL should come from an environment variable but this requires // validating and parsing of the connection url into it's base components. store, err := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte(config.Config.Session_Secret)) if err != nil { log.Fatalln("Failed to connect to Redis.", err) } r.Use( secure.Secure(secure.Options{ // TODO: we should get these from config AllowedHosts: []string{}, SSLRedirect: false, SSLHost: "", SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, STSSeconds: 315360000, STSIncludeSubdomains: true, FrameDeny: true, ContentTypeNosniff: true, BrowserXssFilter: true, ContentSecurityPolicy: "default-src 'self'", }), sessions.Sessions("session", store), auth.UserMiddleware(), ) }
func main() { r := gin.Default() store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret")) r.Use(sessions.Sessions("mysession", store)) r.GET("/incr", func(c *gin.Context) { session := sessions.Default(c) var count int v := session.Get("count") if v == nil { count = 0 } else { count = v.(int) count += 1 } session.Set("count", count) session.Save() c.JSON(200, gin.H{"count": count}) }) r.Run(":8000") }
func session(config *config.Config) (s sessions.Store, err error) { secret := []byte(config.UString("secret", "go-default-secret")) t := config.UString("type", "cookie") switch t { case "cookie": s = sessions.NewCookieStore(secret) case "redis": s, err = sessions.NewRedisStore( config.UInt("size"), config.UString("network"), config.UString("address"), config.UString("password"), secret, ) default: err = errors.New("No session type allowed.") } if err != nil { return nil, err } o, _ := config.Get("options") if o != nil { s.Options(sessions.Options{ Path: o.UString("path"), Domain: o.UString("domain"), MaxAge: o.UInt("max_age", int(30*time.Minute)), Secure: o.UBool("secure"), HttpOnly: o.UBool("http_only"), }) } return s, nil }