ginsm

package
v3.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: MIT, MIT Imports: 4 Imported by: 0

README

forked from go-session/gin-session

go get -u github.com/gandaldf/session/v3

Session middleware for Gin

Build Codecov ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v github.com/go-session/gin-session
Create file server.go
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-session/gin-session"
)

func main() {
	app := gin.Default()

	app.Use(ginsession.New())

	app.GET("/", func(ctx *gin.Context) {
		store := ginsession.FromContext(ctx)
		store.Set("foo", "bar")
		err := store.Save()
		if err != nil {
			ctx.AbortWithError(500, err)
			return
		}

		ctx.Redirect(302, "/foo")
	})

	app.GET("/foo", func(ctx *gin.Context) {
		store := ginsession.FromContext(ctx)
		foo, ok := store.Get("foo")
		if !ok {
			ctx.AbortWithStatus(404)
			return
		}
		ctx.String(http.StatusOK, "foo:%s", foo)
	})

	app.Run(":8080")
}
Build and run
$ go build server.go
$ ./server
Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Lyric

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultConfig is the default Recover middleware config.
	DefaultConfig = Config{
		ErrorHandleFunc: func(ctx *gin.Context, err error) {
			ctx.AbortWithError(500, err)
		},
		StoreKey:  "github.com/go-session/gin-session/store",
		ManageKey: "github.com/go-session/gin-session/manage",
		Skipper: func(_ *gin.Context) bool {
			return false
		},
	}
)

Functions

func Destroy

func Destroy(ctx *gin.Context) error

Destroy a session

func FromContext

func FromContext(ctx *gin.Context) session.Store

FromContext Get session storage from context

func New

func New(opt ...session.Option) gin.HandlerFunc

New create a session middleware

func NewWithConfig

func NewWithConfig(config Config, opt ...session.Option) gin.HandlerFunc

NewWithConfig create a session middleware

func Refresh

func Refresh(ctx *gin.Context) (session.Store, error)

Refresh a session and return to session storage

Types

type Config

type Config struct {
	// error handling when starting the session
	ErrorHandleFunc ErrorHandleFunc
	// keys stored in the context
	StoreKey string
	// keys stored in the context
	ManageKey string
	// defines a function to skip middleware.Returning true skips processing
	// the middleware.
	Skipper func(*gin.Context) bool
}

Config defines the config for Session middleware

type ErrorHandleFunc

type ErrorHandleFunc func(*gin.Context, error)

ErrorHandleFunc error handling function

Jump to

Keyboard shortcuts

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