https://github.com/phwoolcon/gin-utils
Utilities for Gin Web Framework
https://github.com/phwoolcon/gin-utils
Last synced: 24 days ago
JSON representation
Utilities for Gin Web Framework
- Host: GitHub
- URL: https://github.com/phwoolcon/gin-utils
- Owner: phwoolcon
- License: apache-2.0
- Created: 2019-09-25T07:10:34.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-26T23:42:16.000Z (almost 5 years ago)
- Last Synced: 2025-02-28T23:38:41.499Z (over 1 year ago)
- Language: Go
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Utilities for Gin Web Framework
==
## File-based Session
The [Gin middleware for session management](https://github.com/gin-contrib/sessions) has backends like `cookie-based`, `Redis`, `memcached`, `MongoDB`, `memstore`.
But what if you want a `file-based` session?
```go
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")
}
```