Не могу разобраться как работает модуль сессий в beego. (github.com/astaxie/beego/session)
session.go
package authSession
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/session"
)
var (
GlobalSessions *session.Manager
conf = &session.ManagerConfig{}
)
func Init() {
config := `{"cookieName":"gosessionid", "gclifetime":3600, "enableSetCookie":true, "providerConfig":"./tmp"}`
if err := json.Unmarshal([]byte(config), conf); err != nil {
fmt.Println("json decode error", err)
}
GlobalSessions, _ = session.NewManager("file", conf)
go GlobalSessions.GC()
}
Потом в main.go
package main
import (
"github.com/astaxie/beego"
sess "github.com/ProjectDir/authSession"
_ "github.com/ProjectDir/routers"
)
func main() {
sess.Init()
beego.Run()
}
Роуты router.go
package routers
import (
"github.com/astaxie/beegor"
"github.com/ProjectDir/controllers"
)
var (
contr = &controllers.MainController{}
)
func init() {
beego.Router("/", contr)
beego.Router("/login", contr, "get:LoginHandler")
beego.Router("/login", contr, "post:PostLoginHandler")
}
Потом в контроллере default.go
package controllers
import (
"fmt"
"github.com/astaxie/beego"
s "github.com/ProjectDir/authSession"
"github.com/ProjectDir/db/documents"
"github.com/ProjectDir/models"
"github.com/ProjectDir/models/db"
"github.com/ProjectDir/utils"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["title"] = "Home page"
c.TplName = "index.html"
}
func (c *MainController) LoginHandler() {
c.Data["title"] = "Login"
c.TplName = "login.html"
}
func (c *MainController) PostLoginHandler() {
username := c.GetString("username")
password := c.GetString("password")
if username == "" && password == "" {
c.LoginHandler()
return
}
sess, err := s.GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
if err != nil {
panic(err)
}
defer sess.SessionRelease(c.Ctx.ResponseWriter)
err = sess.Set("username", username)
err = sess.Set("IsAutorized", true)
if err != nil {
panic(err)
}
c.Data["isAutorized"] = sess.Get("IsAutorized")
c.Data["title"] = "Login"
c.TplName = "index.html"
c.CruSession = sess
c.Get()
}
В c.Get() нет ничего от сессии
Это после выполнения PostLoginHandler
fmt.Printf("%#v\n%#v\n%#v\n%#v\n", sess, username, password, c)
&session.FileSessionStore{sid:"10a706cb14c68b8fac778a0d1df85512", lock:sync.RWMutex{w:sync.Mutex{state:0, sema:0x0}, writerSem:0x0, readerSem:0x0, readerCount:0, readerWait:0}, values:map[interface {}]interface {}{"username":"1", "IsAutorized":true}}
"1"
"2"
&controllers.MainController{Controller:beego.Controller{Ctx:(*context.Context)(0xc420184b10), Data:map[interface {}]interface {}{"RouterPattern":"/login", "isAutorized":true, "title":"Login"}, controllerName:"MainController", actionName:"PostLoginHandler", methodMapping:map[string]func(){}, gotofunc:"", AppController:(*controllers.MainController)(0xc420304620), TplName:"index.html", Layout:"", LayoutSections:map[string]string(nil), TplPrefix:"", TplExt:"tpl", EnableRender:true, _xsrfToken:"", XSRFExpire:0, EnableXSRF:true, CruSession:(*session.FileSessionStore)(0xc42039c870)}}
<nil>
[SESSION]<nil>
А это на любой другой странице
fmt.Printf("%#v\n", c)
&controllers.MainController{Controller:beego.Controller{Ctx:(*context.Context)(0xc4202a3170), Data:map[interface {}]interface {}{"RouterPattern":"/"}, controllerName:"MainController", actionName:"GET", methodMapping:map[string]func(){}, gotofunc:"", AppController:(*controllers.MainController)(0xc4200dd340), TplName:"", Layout:"", LayoutSections:map[string]string(nil), TplPrefix:"", TplExt:"tpl", EnableRender:true, _xsrfToken:"", XSRFExpire:0, EnableXSRF:true, CruSession:session.Store(nil)}}