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

# 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)
}

# Packages

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

# Functions

default sqlite database store */.
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
seconds, default 7 days.
No description provided by the author

# Structs

No description provided by the author

# Interfaces

No description provided by the author