本文整理汇总了Golang中code/google/com/p/gorilla/sessions.Session类的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Login
func Login(r *http.Request, sess *sessions.Session) (body *shared.Body, tpl *template.Template, redirect string) {
// log.Println("route: login")
var id uint64
if _, ok := sess.Values["id"]; ok {
id = sess.Values["id"].(uint64)
}
if id == 0 {
if r.Method != "POST" {
body, tpl = loginForm(r)
} else {
id, err := authenticate(r)
if err != nil {
body, tpl = loginForm(r)
} else {
sess.Values["id"] = id
fs := sess.Flashes("redirect")
redirect = "/"
if len(fs) > 0 {
redirect = fs[0].(string)
}
}
}
} else {
redirect = "/"
}
return
}
开发者ID:eramus,项目名称:discuss,代码行数:27,代码来源:controller.go
示例2: Reply
func Reply(file string, w http.ResponseWriter, r *http.Request, session *sessions.Session) {
buf := bufio.NewWriter(w)
t, _ := template.ParseFiles(file)
t.Execute(buf, session)
session.Save(r, w)
buf.Flush()
}
开发者ID:errnoh,项目名称:wepa2012,代码行数:7,代码来源:controllers.go
示例3: Save
// Save adds a single session to the response.
func (s *redisStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
var err error
if session.ID == "" {
var i uint64
i, err = NoeqClient.GenOne()
if err != nil {
return err
}
session.ID = strconv.FormatUint(i, 10)
}
if err = s.save(session); err != nil {
return err
}
var encoded string
encoded, err = securecookie.EncodeMulti(session.Name(), &session.ID, s.Codecs...)
if err != nil {
return err
}
options := s.Options
if session.Options != nil {
options = session.Options
}
cookie := &http.Cookie{
Name: session.Name(),
Value: encoded,
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HttpOnly,
}
http.SetCookie(w, cookie)
context.DefaultContext.Clear(r)
return nil
}
开发者ID:eramus,项目名称:discuss,代码行数:36,代码来源:session.go
示例4: load
func (s *MongoStore) load(session *sessions.Session) error {
mg := &MgSessionTbl{SessionID: []byte(session.ID)}
err := s.DBCollection.Find(bson.M{"sessionid": []byte(session.ID)}).One(mg)
if err == nil {
err = securecookie.DecodeMulti(session.Name(),
string(mg.Encoded), &session.Values, s.Codecs...)
}
return err
}
开发者ID:scotch,项目名称:sapling-aego,代码行数:9,代码来源:mongodb.go
示例5: Logout
func Logout(r *http.Request, sess *sessions.Session) (body *shared.Body, tpl *template.Template, redirect string) {
// log.Println("route: logout")
sess.Values["id"] = uint64(0)
redirect = "/"
fs := sess.Flashes("last")
if len(fs) != 0 {
redirect = fs[0].(string)
}
return
}
开发者ID:eramus,项目名称:discuss,代码行数:10,代码来源:controller.go
示例6: save
func (s *MongoStore) save(session *sessions.Session) error {
encoded, err := securecookie.EncodeMulti(session.Name(),
session.Values,
s.Codecs...)
if err != nil {
return err
}
mg := &MgSessionTbl{
Encoded: encoded,
SessionID: []byte(session.ID),
Age: bson.Now(),
}
_, err = s.DBCollection.Upsert(bson.M{"sessionid": session.ID}, mg)
return err
}
开发者ID:scotch,项目名称:sapling-aego,代码行数:15,代码来源:mongodb.go
示例7: Save
// Save adds a single session to the response.
func (s *DatastoreStore) Save(r *http.Request, w http.ResponseWriter,
session *sessions.Session) error {
if session.ID == "" {
session.ID = string(securecookie.GenerateRandomKey(32))
}
if err := s.save(r, session); err != nil {
return err
}
encoded, err := securecookie.EncodeMulti(session.Name(), session.ID,
s.Codecs...)
if err != nil {
return err
}
http.SetCookie(w, sessions.NewCookie(session.Name(), encoded,
session.Options))
return nil
}
开发者ID:shawnps,项目名称:kanjihub,代码行数:18,代码来源:sessions.go
示例8: killSession
func killSession(r *http.Request, w http.ResponseWriter, s *sessions.Session) error {
d := make(chan bool)
kills <- &sessionAction{"session:" + s.ID, 0, nil, d}
f := <-d
if !f {
return cantKill
}
var opts = *sessionStore.Options
opts.MaxAge = -1
s.Options = &opts
// values
for k, _ := range s.Values {
delete(s.Values, k)
}
s.Save(r, w)
return nil
}
开发者ID:eramus,项目名称:discuss,代码行数:17,代码来源:session.go
示例9: load
// load reads a file and decodes its content into session.Values.
func (s *redisStore) load(session *sessions.Session) error {
if session.Name() == "remember" {
return nil
}
key := "session:" + session.ID
se, rerr := RedisClient.Get(key)
if rerr != nil {
return rerr
}
ss := se.String()
if ss == "" {
return nil
}
err := securecookie.DecodeMulti(session.Name(), ss, &session.Values, s.Codecs...)
if err != nil {
return err
}
return nil
}
开发者ID:eramus,项目名称:discuss,代码行数:20,代码来源:session.go
示例10: save
// save writes encoded session.Values to a file.
func (s *redisStore) save(session *sessions.Session) error {
if session.Name() == "remember" {
return nil
}
if len(session.Values) == 0 {
// Don't need to write anything.
return nil
}
encoded, err := securecookie.EncodeMulti(session.Name(), &session.Values, s.Codecs...)
if err != nil {
return err
}
d := make(chan bool)
saves <- &sessionAction{"session:" + session.ID, int64(sessionExpire), encoded, d}
f := <-d
if !f {
return cantSave
}
return nil
}
开发者ID:eramus,项目名称:discuss,代码行数:21,代码来源:session.go
示例11: New
// New returns a session for the given name without adding it to the registry.
//
// See CookieStore.New().
func (s *redisStore) New(r *http.Request, name string) (*sessions.Session, error) {
var c *http.Cookie
var err error
c, err = r.Cookie(name)
if err != nil && err != http.ErrNoCookie {
return nil, err
}
var session *sessions.Session
session = sessions.NewSession(s, name)
session.IsNew = true
if c != nil {
securecookie.DecodeMulti(name, c.Value, &session.ID, s.Codecs...)
s.load(session)
if len(session.Values) > 0 {
session.IsNew = false
}
}
return session, nil
}
开发者ID:eramus,项目名称:discuss,代码行数:23,代码来源:session.go
示例12: testSessionFlashes
func testSessionFlashes(t *testing.T, store sessions.Store) {
defer context.Close()
var req *http.Request
var rsp *ResponseRecorder
var hdr http.Header
var err error
var ok bool
var cookies []string
var session *sessions.Session
var flashes []interface{}
// Round 1 ----------------------------------------------------------------
req = getRequest()
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Get a flash.
flashes = session.Flashes()
if len(flashes) != 0 {
t.Errorf("Expected empty flashes; Got %v", flashes)
}
// Add some flashes.
session.AddFlash("foo")
session.AddFlash("bar")
// Custom key.
session.AddFlash("baz", "custom_key")
// Save.
if err = sessions.Save(req, rsp); err != nil {
t.Fatalf("Error saving session: %v", err)
}
hdr = rsp.Header()
cookies, ok = hdr["Set-Cookie"]
if !ok || len(cookies) != 1 {
t.Fatalf("No cookies. Header:", hdr)
}
// Round 2 ----------------------------------------------------------------
req = getRequest()
req.Header.Add("Cookie", cookies[0])
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Check all saved values.
flashes = session.Flashes()
if len(flashes) != 2 {
t.Fatalf("Expected flashes; Got %v", flashes)
}
if flashes[0] != "foo" || flashes[1] != "bar" {
t.Errorf("Expected foo,bar; Got %v", flashes)
}
flashes = session.Flashes()
if len(flashes) != 0 {
t.Errorf("Expected dumped flashes; Got %v", flashes)
}
// Custom key.
flashes = session.Flashes("custom_key")
if len(flashes) != 1 {
t.Errorf("Expected flashes; Got %v", flashes)
} else if flashes[0] != "baz" {
t.Errorf("Expected baz; Got %v", flashes)
}
flashes = session.Flashes("custom_key")
if len(flashes) != 0 {
t.Errorf("Expected dumped flashes; Got %v", flashes)
}
}
开发者ID:shawnps,项目名称:kanjihub,代码行数:73,代码来源:sessions_test.go
注:本文中的code/google/com/p/gorilla/sessions.Session类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论