phwoolcon / gin-utils

Utilities for Gin Web Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Utilities for Gin Web Framework

File-based Session

The Gin middleware for session management has backends like cookie-based, Redis, memcached, MongoDB, memstore.

But what if you want a file-based session?

package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	psession "github.com/phwoolcon/gin-utils/session"
)

func main() {
	engine := gin.Default()
	apiRouter := engine.Group("/api")
	fsStore := psession.NewFileStore("./data/session", []byte("secret"))
	apiRouter.Use(sessions.Sessions("auth", fsStore))

	apiRouter.GET("/hello", func(context *gin.Context) {
		session := sessions.Default(context)

		if session.Get("hello") != "world" {
			session.Set("hello", "world")
			session.Save()
		}

		context.JSON(200, gin.H{"hello": session.Get("hello")})
	})
	engine.Run(":8000")
}

About

Utilities for Gin Web Framework

License:Apache License 2.0


Languages

Language:Go 100.0%