1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 17:30:56 +00:00

add session support

This commit is contained in:
xiemengjun 2013-01-01 23:11:15 +08:00
parent 1f67fcb0f9
commit 1530e3fbc2
2 changed files with 45 additions and 0 deletions

View File

@ -2,9 +2,12 @@ package beego
import ( import (
"fmt" "fmt"
"github.com/astaxie/session"
_ "github.com/astaxie/session/providers/memory"
"net/http" "net/http"
"os" "os"
"path" "path"
"strconv"
) )
var ( var (
@ -20,6 +23,13 @@ var (
ViewsPath string ViewsPath string
RunMode string //"dev" or "prod" RunMode string //"dev" or "prod"
AppConfig *Config AppConfig *Config
//related to session
SessionOn bool // wheather auto start session,default is false
SessionProvider string // default session provider memory
SessionName string // sessionName cookie's name
SessionGCMaxLifetime int64 // session's gc maxlifetime
GlobalSessions *session.Manager //GlobalSessions
) )
func init() { func init() {
@ -38,6 +48,10 @@ func init() {
RecoverPanic = true RecoverPanic = true
PprofOn = false PprofOn = false
ViewsPath = "views" ViewsPath = "views"
SessionOn = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
} else { } else {
HttpAddr = AppConfig.String("httpaddr") HttpAddr = AppConfig.String("httpaddr")
if v, err := AppConfig.Int("httpport"); err != nil { if v, err := AppConfig.Int("httpport"); err != nil {
@ -71,6 +85,27 @@ func init() {
} else { } else {
ViewsPath = views ViewsPath = views
} }
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 {
int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64)
SessionGCMaxLifetime = int64val
} else {
SessionGCMaxLifetime = 3600
}
} }
StaticDir["/static"] = "static" StaticDir["/static"] = "static"
@ -158,5 +193,9 @@ func Run() {
BeeApp.RegisterController(`/debug/pprof`, &ProfController{}) BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{}) BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
} }
if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
go GlobalSessions.GC()
}
BeeApp.Run() BeeApp.Run()
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"github.com/astaxie/session"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -151,3 +152,8 @@ func (c *Controller) Input() url.Values {
c.Ctx.Request.ParseForm() c.Ctx.Request.ParseForm()
return c.Ctx.Request.Form return c.Ctx.Request.Form
} }
func (c *Controller) StartSession() (sess session.Session) {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
return
}