Skip to content

Instantly share code, notes, and snippets.

@mishudark
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save mishudark/4bde9595b673015d83eb to your computer and use it in GitHub Desktop.

Select an option

Save mishudark/4bde9595b673015d83eb to your computer and use it in GitHub Desktop.
Session support for beego

#Beego Session

this patch adds session support to beego framework

diff --git a/beego.go b/beego.go
index e648480..5ddedc7 100644
--- a/beego.go
+++ b/beego.go
@@ -352,7 +352,7 @@ func initBeforeHttpRun() {
}
}
- if SessionOn {
+ if SessionOn || UseSessions {
var err error
sessionConfig := AppConfig.String("sessionConfig")
if sessionConfig == "" {
diff --git a/config.go b/config.go
index 1984e3c..cba42df 100644
--- a/config.go
+++ b/config.go
@@ -48,6 +48,7 @@ var (
AppConfig config.ConfigContainer
GlobalSessions *session.Manager // global session mananger
SessionOn bool // flag of starting session auto. default is false.
+ UseSessions bool // flag of application uses sessions. default is false.
SessionProvider string // default session provider, memory, mysql , redis ,etc.
SessionName string // the cookie name when saving session id into cookie.
SessionGCMaxLifetime int64 // session gc time for auto cleaning expired session.
@@ -125,6 +126,7 @@ func init() {
ViewsPath = "views"
SessionOn = false
+ UseSessions = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
@@ -224,6 +226,10 @@ func ParseConfig() (err error) {
SessionOn = sessionon.(bool)
}
+ if usesessions, err := getConfig("bool", "UseSessions"); err == nil {
+ UseSessions = usesessions.(bool)
+ }
+
if sessProvider, _ := getConfig("string", "SessionProvider"); sessProvider != "" {
SessionProvider = sessProvider.(string)
}
diff --git a/controller.go b/controller.go
index 6f7b510..7163f51 100644
--- a/controller.go
+++ b/controller.go
@@ -415,6 +415,14 @@ func (c *Controller) SaveToFile(fromfile, tofile string) error {
// StartSession starts session and load old session data info this controller.
func (c *Controller) StartSession() session.SessionStore {
if c.CruSession == nil {
+ if c.Ctx.Input.CruSession == nil {
+ r := c.Ctx.Request
+ w := c.Ctx.ResponseWriter
+ c.Ctx.Input.CruSession = GlobalSessions.SessionStart(w, r)
+ defer func() {
+ c.Ctx.Input.CruSession.SessionRelease(w)
+ }()
+ }
c.CruSession = c.Ctx.Input.CruSession
}
return c.CruSession
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment