当前位置: 首页>>代码示例>>Golang>>正文


Golang Session.Flashes方法代码示例

本文整理汇总了Golang中code/google/com/p/gorilla/sessions.Session.Flashes方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.Flashes方法的具体用法?Golang Session.Flashes怎么用?Golang Session.Flashes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在code/google/com/p/gorilla/sessions.Session的用法示例。


在下文中一共展示了Session.Flashes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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

示例3: 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.Flashes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。