add session support
Showing
2 changed files
with
45 additions
and
0 deletions
| ... | @@ -2,9 +2,12 @@ package beego | ... | @@ -2,9 +2,12 @@ package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | "github.com/astaxie/session" | ||
| 6 | _ "github.com/astaxie/session/providers/memory" | ||
| 5 | "net/http" | 7 | "net/http" |
| 6 | "os" | 8 | "os" |
| 7 | "path" | 9 | "path" |
| 10 | "strconv" | ||
| 8 | ) | 11 | ) |
| 9 | 12 | ||
| 10 | var ( | 13 | var ( |
| ... | @@ -20,6 +23,13 @@ var ( | ... | @@ -20,6 +23,13 @@ var ( |
| 20 | ViewsPath string | 23 | ViewsPath string |
| 21 | RunMode string //"dev" or "prod" | 24 | RunMode string //"dev" or "prod" |
| 22 | AppConfig *Config | 25 | AppConfig *Config |
| 26 | //related to session | ||
| 27 | SessionOn bool // wheather auto start session,default is false | ||
| 28 | SessionProvider string // default session provider memory | ||
| 29 | SessionName string // sessionName cookie's name | ||
| 30 | SessionGCMaxLifetime int64 // session's gc maxlifetime | ||
| 31 | |||
| 32 | GlobalSessions *session.Manager //GlobalSessions | ||
| 23 | ) | 33 | ) |
| 24 | 34 | ||
| 25 | func init() { | 35 | func init() { |
| ... | @@ -38,6 +48,10 @@ func init() { | ... | @@ -38,6 +48,10 @@ func init() { |
| 38 | RecoverPanic = true | 48 | RecoverPanic = true |
| 39 | PprofOn = false | 49 | PprofOn = false |
| 40 | ViewsPath = "views" | 50 | ViewsPath = "views" |
| 51 | SessionOn = false | ||
| 52 | SessionProvider = "memory" | ||
| 53 | SessionName = "beegosessionID" | ||
| 54 | SessionGCMaxLifetime = 3600 | ||
| 41 | } else { | 55 | } else { |
| 42 | HttpAddr = AppConfig.String("httpaddr") | 56 | HttpAddr = AppConfig.String("httpaddr") |
| 43 | if v, err := AppConfig.Int("httpport"); err != nil { | 57 | if v, err := AppConfig.Int("httpport"); err != nil { |
| ... | @@ -71,6 +85,27 @@ func init() { | ... | @@ -71,6 +85,27 @@ func init() { |
| 71 | } else { | 85 | } else { |
| 72 | ViewsPath = views | 86 | ViewsPath = views |
| 73 | } | 87 | } |
| 88 | if ar, err := AppConfig.Bool("sessionon"); err != nil { | ||
| 89 | SessionOn = false | ||
| 90 | } else { | ||
| 91 | SessionOn = ar | ||
| 92 | } | ||
| 93 | if ar := AppConfig.String("sessionprovider"); ar == "" { | ||
| 94 | SessionProvider = "memory" | ||
| 95 | } else { | ||
| 96 | SessionProvider = ar | ||
| 97 | } | ||
| 98 | if ar := AppConfig.String("sessionname"); ar == "" { | ||
| 99 | SessionName = "beegosessionID" | ||
| 100 | } else { | ||
| 101 | SessionName = ar | ||
| 102 | } | ||
| 103 | if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err != nil { | ||
| 104 | int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64) | ||
| 105 | SessionGCMaxLifetime = int64val | ||
| 106 | } else { | ||
| 107 | SessionGCMaxLifetime = 3600 | ||
| 108 | } | ||
| 74 | } | 109 | } |
| 75 | StaticDir["/static"] = "static" | 110 | StaticDir["/static"] = "static" |
| 76 | 111 | ||
| ... | @@ -158,5 +193,9 @@ func Run() { | ... | @@ -158,5 +193,9 @@ func Run() { |
| 158 | BeeApp.RegisterController(`/debug/pprof`, &ProfController{}) | 193 | BeeApp.RegisterController(`/debug/pprof`, &ProfController{}) |
| 159 | BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{}) | 194 | BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{}) |
| 160 | } | 195 | } |
| 196 | if SessionOn { | ||
| 197 | GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime) | ||
| 198 | go GlobalSessions.GC() | ||
| 199 | } | ||
| 161 | BeeApp.Run() | 200 | BeeApp.Run() |
| 162 | } | 201 | } | ... | ... |
| ... | @@ -4,6 +4,7 @@ import ( | ... | @@ -4,6 +4,7 @@ import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "encoding/json" | 5 | "encoding/json" |
| 6 | "encoding/xml" | 6 | "encoding/xml" |
| 7 | "github.com/astaxie/session" | ||
| 7 | "html/template" | 8 | "html/template" |
| 8 | "io/ioutil" | 9 | "io/ioutil" |
| 9 | "net/http" | 10 | "net/http" |
| ... | @@ -151,3 +152,8 @@ func (c *Controller) Input() url.Values { | ... | @@ -151,3 +152,8 @@ func (c *Controller) Input() url.Values { |
| 151 | c.Ctx.Request.ParseForm() | 152 | c.Ctx.Request.ParseForm() |
| 152 | return c.Ctx.Request.Form | 153 | return c.Ctx.Request.Form |
| 153 | } | 154 | } |
| 155 | |||
| 156 | func (c *Controller) StartSession() (sess session.Session) { | ||
| 157 | sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) | ||
| 158 | return | ||
| 159 | } | ... | ... |
-
Please register or sign in to post a comment