Files
build-web-application-with-…/en/14.2.md
2016-09-27 16:12:42 -03:00

3.5 KiB

14.2 Sessions

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.

Integrating sessions

Beego handles sessions mainly according to the following global variables:

// related to session
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 use the values from the following configuration file code to set these values:

if ar, err := AppConfig.Bool("sessionon"); err != nil {
	SessionOn = false
} else {
	SessionOn = ar
}
if ar := AppConfig.String("sessionprovider"); ar == "" {
	SessionProvider = "memory"
} else {
	SessionProvider = ar
}
if ar := AppConfig.String("sessionname"); ar == "" {
	SessionName = "beegosessionID"
} else {
	SessionName = ar
}
if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err != nil && ar != 0 {
	int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64)
	SessionGCMaxLifetime = int64val
} else {
	SessionGCMaxLifetime = 3600
}

Add the following code in the beego.Run function:

if SessionOn {
	GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
	go GlobalSessions.GC()
}

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
}		

Session using

Through the above code we can see, beego framework simply inherit the session function, then how to use it in your project ?

First, we need to apply the main entrance open session:

beego.SessionOn = true

We can then corresponding method in the controller to use the session as follows: the

func (this *MainController) Get() {
	var intcount int
	sess := this.StartSession()
	count := sess.Get("count")
	if count == nil {
		intcount = 0
	} else {
		intcount = count.(int)
	}
	intcount = intcount + 1
	sess.Set("count", intcount)
	this.Data["Username"] = "astaxie"
	this.Data["Email"] = "astaxie@gmail.com"
	this.Data["Count"] = intcount
	this.TplNames = "index.tpl"
}

The above code shows how to use the control logic session, mainly divided into two steps:

  1. Get session object

     // Get the object, similar in PHP session_start()
     sess:= this.StartSession()
    
  2. to use the session for general session value operation

     // Get the session values , similar in PHP $ _SESSION ["count"]
     sess.Get("count")
    
     // Set the session value
     sess.Set("count", intcount)
    

As can be seen from the above code beego framework based applications developed using the session quite easy, basically, and PHP to call session_start() similar.