ginsession

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2019 License: MIT Imports: 11 Imported by: 0

README

Go Report Card

ginsession

A session middleware for gin framework.

Usage

Download and install:

go get github.com/Q1mi/ginsession

Import it in you code:

import "github.com/Q1mi/ginsession"

Examples

Redis-based

package main

import (
	"fmt"
	"log"

	"net/http"

	"github.com/Q1mi/ginsession"
	"github.com/gin-gonic/gin"
)


func main(){
	r := gin.Default()
	mgrObj, err := ginsession.CreateSessionMgr("redis", "localhost:6379")
	if err != nil {
		log.Fatalf("create manager obj failed, err:%v\n", err)
		return
	}
	sm := ginsession.SessionMiddleware(mgrObj, ginsession.Options{
		Path: "/",
		Domain: "127.0.0.1",
		MaxAge: 60,
		Secure:false,
		HttpOnly:true,
	})

	r.Use(sm)

	r.GET("/incr", func(c *gin.Context) {
		session := c.MustGet("session").(ginsession.Session)
		fmt.Printf("%#v\n", session)
		var count int
		v, err := session.Get("count")
		if err != nil{
			log.Printf("get count from session failed, err:%v\n", err)
			count = 0
		} else {
			count = v.(int)
			count++
		}
		session.Set("count", count)
		session.Save()
		c.String(http.StatusOK, "count:%v", count)
	})

	r.Run()
}
memory_based:

warning: this is just for test!


package main

import (
	"fmt"
	"log"

	"net/http"

	"github.com/Q1mi/ginsession"
	"github.com/gin-gonic/gin"
)


func main(){
	r := gin.Default()
	mgrObj, err := ginsession.CreateSessionMgr("memory", "")
	if err != nil {
		log.Fatalf("create manager obj failed, err:%v\n", err)
		return
	}
	sm := ginsession.SessionMiddleware(mgrObj, ginsession.Options{
		Path: "/",
		Domain: "127.0.0.1",
		MaxAge: 60,
		Secure:false,
		HttpOnly:true,
	})

	r.Use(sm)

	r.GET("/incr", func(c *gin.Context) {
		session := c.MustGet("session").(ginsession.Session)
		fmt.Printf("%#v\n", session)
		var count int
		v, err := session.Get("count")
		if err != nil{
			log.Printf("get count from session failed, err:%v\n", err)
			count = 0
		} else {
			count = v.(int)
			count++
		}
		session.Set("count", count)
		session.Save()
		c.String(http.StatusOK, "count:%v", count)
	})

	r.Run()
}

TODO

  1. Add more support...

Documentation

Index

Constants

View Source
const (
	// SessionCookieName key of SessionID store in Cookie
	SessionCookieName = "session_id"
	// SessionName key of session in gin.context
	SessionContextName = "session"
)

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware() gin.HandlerFunc

AuthMiddleware 认证中间件 从请求的session Data中获取isLogin

func NewMemSession

func NewMemSession(id string) *memSession

NewMemSession constructor

func NewRedisSessionMgr

func NewRedisSessionMgr() *redisSessionMgr

NewRedisSessionMgr redis-based sessionMgr constructor

func SessionMiddleware

func SessionMiddleware(sm SessionMgr, options Options) gin.HandlerFunc

SessionMiddleware gin middleware

Types

type MemSessionMgr

type MemSessionMgr struct {
	// contains filtered or unexported fields
}

MemSessionMgr a memory-based manager

func NewMemSessionMgr

func NewMemSessionMgr() *MemSessionMgr

NewMemSessionMgr constructor

func (*MemSessionMgr) Clear

func (m *MemSessionMgr) Clear(sessionID string)

Clear delete the request session in sessionMgr

func (*MemSessionMgr) CreateSession

func (m *MemSessionMgr) CreateSession() (sd Session)

CreateSession create a new session

func (*MemSessionMgr) GetSession

func (m *MemSessionMgr) GetSession(sessionID string) (sd Session, err error)

GetSession get the session by sessionID

func (*MemSessionMgr) Init

func (m *MemSessionMgr) Init(addr string, options ...string) (err error)

Init init...

type Options

type Options struct {
	Path   string
	Domain string
	// MaxAge=0 means no 'Max-Age' attribute specified.
	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
	// MaxAge>0 means Max-Age attribute present and given in seconds.
	MaxAge   int
	Secure   bool
	HttpOnly bool
}

Options Cookie Options

type Session

type Session interface {
	ID() string
	Load() error // load
	Get(string) (interface{}, error)
	Set(string, interface{})
	Del(string)
	Save()
	SetExpired(int)
}

Session stores values for a session

func NewRedisSession

func NewRedisSession(id string, client *redis.Client) (session Session)

NewRedisSession redisSession constructor

type SessionMgr

type SessionMgr interface {
	Init(addr string, options ...string) error // init the session store
	GetSession(string) (Session, error)        // get the session by sessionID
	CreateSession() Session                    // create a new session
	Clear(string)                              // clear the request session data
}

SessionMgr a session manager

func CreateSessionMgr

func CreateSessionMgr(name string, addr string, options ...string) (sm SessionMgr, err error)

CreateSessionMgr create a sessionMgr by given name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL