Skip to content

phwoolcon/gin-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages