session.go 627 B

123456789101112131415161718192021222324252627
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package middleware
  4. import (
  5. "github.com/gin-contrib/sessions"
  6. "github.com/gin-contrib/sessions/cookie"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func SessionConfig() sessions.Store {
  10. sessionMaxAge := 3600
  11. sessionSecret := "smart"
  12. var store sessions.Store
  13. store = cookie.NewStore([]byte(sessionSecret))
  14. store.Options(sessions.Options{
  15. MaxAge: sessionMaxAge, //seconds
  16. Path: "/",
  17. })
  18. return store
  19. }
  20. func Session(keyPairs string) gin.HandlerFunc {
  21. store := SessionConfig()
  22. return sessions.Sessions(keyPairs, store)
  23. }