gin_session

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 7 Imported by: 0

README

gin-session

A simple gin session middleware, currently supports redis, gorm store backend.

Install

go get github.com/dev-shao/gin-session

Quick start

package main

import (
    "github.com/dev-shao/gin-session"
    "github.com/gin-gonic/gin"
)

func main(){
    //gin router
    router := gin.Default()
    //default sqlite3 database store
    router.Use(gin_session.Default())
    router.GET("/test", func(context *gin.Context) {
        //init session from context
        session := gin_session.From(context)
        //set
        session.Set("key","value")
        //get
        session.Get("key")
        //delete
        session.Delete("key")
        //save
        session.Save()
    })
}

Store Backend

GormStore

default: sqlite3

//default GormStore, sqlite3 database
middlware := gin_session.Defalult()

//等同于
import (
    "gorm.io/gorm"
    "gorm.io/driver/sqlite"
    "github.com/dev-shao/gin-session"
    "github.com/dev-shao/gin-session/gorm-store"
)

func main(){
    db, _ := gorm.Open(sqlite.Open("./sessions.db"))
    store := gorm_store(db)
    middleware := gin_session.Middleware(store)
}

mysql

import (
    "gorm.io/gorm"
    "gorm.io/driver/mysql"
    "github.com/dev-shao/gin-session"
    "github.com/dev-shao/gin-session/gorm-store"
)

func main(){
    dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn))
    store := gorm_store(db)
    middleware := gin_session.Middleware(store)
}

more database see: gorm document

RedisStore
import (
    "github.com/dev-shao/gin-session"
    "github.com/dev-shao/gin-session/redis-store"
    "github.com/go-redis/redis"
)

func main() {
    opt := redis.Options{
        Addr: "127.0.0.1:6379",
        Password: "",
        DB: 0,
	}
	store := redis_store.New(&opt)
	middleware := gin_session.Middleware(store)
}
MemoryStore

only used in test or development environment

import (
    "github.com/dev-shao/gin-session"
    "github.com/dev-shao/gin-session/memory-store"
)

func main() {
	store := memory_store.New()
	middleware := gin_session.Middleware(store)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SessionCookieName = "SessionId"
View Source
var SessionExpireAge time.Duration = 604800 //seconds, default 7 days
View Source
var SessionKey = "Session"

Functions

func Default

func Default() gin.HandlerFunc

default sqlite database store

func Middleware

func Middleware(store Store) gin.HandlerFunc

Types

type Session

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

func From

func From(ctx *gin.Context) *Session

func New

func New(key string, expire time.Duration, store Store) *Session

func (*Session) Clear

func (self *Session) Clear()

func (*Session) DefaultGet

func (self *Session) DefaultGet(key string, defaultValue interface{}) interface{}

func (*Session) Delete

func (self *Session) Delete(key string)

func (*Session) Get

func (self *Session) Get(key string) interface{}

func (*Session) GetExpireTime

func (self *Session) GetExpireTime() time.Time

func (*Session) Load

func (self *Session) Load() error

func (*Session) Refresh

func (self *Session) Refresh()

refresh key

func (*Session) Save

func (self *Session) Save() error

func (*Session) Set

func (self *Session) Set(key string, value interface{})

func (*Session) SetExpireAge

func (self *Session) SetExpireAge(expire time.Duration)

type Store

type Store interface {
	Get(key string) (map[string]interface{}, error)
	Set(key string, value map[string]interface{}, expire time.Duration) error
	Delete(key string)
	Exists(key string) bool
	GetExpireTime(key string) time.Time
	SetExpireTime(key string, duration time.Duration)
	ClearExpired()
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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