Categorygithub.com/dolfly/gin-session
modulepackage
1.0.1
Repository: https://github.com/dolfly/gin-session.git
Documentation: pkg.go.dev

# README

sessions

Build Status codecov Go Report Card GoDoc Join the chat at https://gitter.im/gin-gonic/gin

Gin middleware for session management with multi-backend support (currently cookie, Redis).

Examples

cookie-based

package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	store := sessions.NewCookieStore([]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++
		}
		session.Set("count", count)
		session.Save()
		c.JSON(200, gin.H{"count": count})
	})
	r.Run(":8000")
}

Redis

package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

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++
		}
		session.Set("count", count)
		session.Save()
		c.JSON(200, gin.H{"count": count})
	})
	r.Run(":8000")
}

Memcache

package main

import (
	"github.com/bradfitz/gomemcache/memcache"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	store := sessions.NewMemcacheStore(memcache.New("localhost:11211"), "", []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++
		}
		session.Set("count", count)
		session.Save()
		c.JSON(200, gin.H{"count": count})
	})
	r.Run(":8000")
}

Mongodb

package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2"
)

func main() {
	r := gin.Default()
	session, err := mgo.Dial("localhost:27017/test")
	if err != nil {
		// handle err
	}

	c := session.DB("").C("sessions")
	store := sessions.NewMongoStore(c, 3600, true, []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++
		}
		session.Set("count", count)
		session.Save()
		c.JSON(200, gin.H{"count": count})
	})
	r.Run(":8000")
}

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

shortcut to get session.
Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.
client: memcache client.
size: maximum number of idle connections.
NewRedisStoreWithDB - like NewRedisStore but accepts `DB` parameter to select redis DB instead of using the default one ("0") Ref: https://godoc.org/github.com/boj/redistore#NewRediStoreWithDB.
NewRedisStoreWithPool instantiates a RediStore with a *redis.Pool passed in.
No description provided by the author

# Constants

No description provided by the author

# Structs

Options stores configuration for a session or session store.

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
Wraps thinly gorilla-session methods.
No description provided by the author