Fix section "integrating sessions" in 14.2.md
This commit is contained in:
21
en/14.2.md
21
en/14.2.md
@@ -2,18 +2,19 @@
|
||||
|
||||
In chapter 6, we introduced some basic concepts pertaining to sessions in Go, and we implemented a sessions manager. The Beego framework uses this session manager to implement some convenient session handling functionality.
|
||||
|
||||
## Session integration
|
||||
beego mainly in the following global variables to control the session handling:
|
||||
## Integrating sessions
|
||||
|
||||
Beego handles sessions mainly according to the following global variables:
|
||||
|
||||
// related to session
|
||||
SessionOn bool // whether to open the session module, the default is not open
|
||||
SessionProvider string // session backend processing module provided, the default is sessionManager supported memory
|
||||
SessionName string // client name saved in cookies
|
||||
SessionGCMaxLifetime int64 // cookies validity
|
||||
SessionOn bool // whether or not to open the session module. Defaults to false.
|
||||
SessionProvider string // the desired session backend processing module. Defaults to an in-memory sessionManager
|
||||
SessionName string // the name of the client saved cookies
|
||||
SessionGCMaxLifetime int64 // cookie validity
|
||||
|
||||
GlobalSessions *session.Manager// global session controller
|
||||
|
||||
Of course, the above values of these variables need to be initialized, you can also follow the code to match the configuration file to set these values:
|
||||
Of course, the above values of these variables need to be initialized. You can also use the values from the following configuration file code to set these values:
|
||||
|
||||
if ar, err := AppConfig.Bool("sessionon"); err != nil {
|
||||
SessionOn = false
|
||||
@@ -37,17 +38,19 @@ Of course, the above values of these variables need to be initialized, you can a
|
||||
SessionGCMaxLifetime = 3600
|
||||
}
|
||||
|
||||
In beego.Run function to add the following code:
|
||||
Add the following code in the `beego.Run` function:
|
||||
|
||||
if SessionOn {
|
||||
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
|
||||
go GlobalSessions.GC()
|
||||
}
|
||||
|
||||
So long SessionOn set to true, then it will open the session by default function to open an independent goroutine to handle session.
|
||||
As long as `SessionOn` is set to true, it will open the session by default with an independent goroutine session handler
|
||||
|
||||
In order to facilitate our custom Controller quickly using session, the author `beego.Controller` provides the following methods:
|
||||
|
||||
To assist us in quickly using sessions in a custom Controller, `beego.Controller` provides the following method:
|
||||
|
||||
func (c *Controller) StartSession() (sess session.Session) {
|
||||
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user