Skip to content

go-session/gin-session

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f93b269 · Jan 17, 2019

History

13 Commits
May 9, 2018
Jun 8, 2018
May 9, 2018
Aug 31, 2018
Jan 17, 2019
Jun 13, 2018

Repository files navigation

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