Wide Session

This commit is contained in:
Liang Ding 2014-09-17 11:17:25 +08:00
parent 5e01f1e394
commit 2fe16c646f
3 changed files with 29 additions and 6 deletions

20
main.go
View File

@ -49,17 +49,18 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := user.HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
httpSessionId := httpSession.Values["id"].(string)
// TODO: 写死以 admin 作为用户登录
name := conf.Wide.Users[0].Name
username := conf.Wide.Users[0].Name
if httpSession.IsNew {
httpSession.Values["username"] = name
httpSessionId := strconv.Itoa(rand.Int())
httpSession.Values["username"] = username
httpSessionId = strconv.Itoa(rand.Int())
httpSession.Values["id"] = httpSessionId
// 一天过期
httpSession.Options.MaxAge = 60 * 60 * 24
glog.Infof("Created a session [%s] for user [%s]", httpSession.Values["id"].(string), name)
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), username)
}
httpSession.Save(r, w)
@ -67,6 +68,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
// Wide 会话关联 HTTP 会话
wideSession.HTTPSessionId = httpSession.Values["id"].(string)
wideSessions := user.WideSessions.GetByHTTPSid(httpSessionId)
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
t, err := template.ParseFiles("view/index.html")
if nil != err {
@ -79,6 +83,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
t.Execute(w, model)
}
// favicon.ico 请求处理.
func faviconHandler(w http.ResponseWriter, r *http.Request) {
// TODO: favicon.ico 请求处理.
}
// 主程序入口.
func main() {
runtime.GOMAXPROCS(conf.Wide.MaxProcs)
@ -87,6 +96,7 @@ func main() {
// 静态资源
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/favicon.ico", faviconHandler)
// 库资源
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))

View File

@ -45,7 +45,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
// 一天过期
httpSession.Options.MaxAge = 60 * 60 * 24
glog.Infof("Created a HTTP session [%s] for user [%s]", session.Values["id"].(string), name)
glog.Infof("Created a HTTP session [%s] for user [%s]", httpSession.Values["id"].(string), name)
}
httpSession.Save(r, w)

View File

@ -60,6 +60,19 @@ func (sessions *Sessions) Remove(sid string) {
}
}
// 获取 HTTP 会话关联的所有 Wide 会话.
func (sessions *Sessions) GetByHTTPSid(httpSessionId string) []*WideSession {
ret := []*WideSession{}
for _, s := range *sessions {
if s.HTTPSessionId == httpSessionId {
ret = append(ret, s)
}
}
return ret
}
// 移除 HTTP 会话关联的所有 Wide 会话.
func (sessions *Sessions) RemoveByHTTPSid(httpSessionId string) {
for i, s := range *sessions {