From 2fe16c646f96180ba8db7e174f398e785c7f1f95 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 17 Sep 2014 11:17:25 +0800 Subject: [PATCH] Wide Session --- main.go | 20 +++++++++++++++----- shell/shells.go | 2 +- user/sessions.go | 13 +++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 73bb72f..df4ec48 100644 --- a/main.go +++ b/main.go @@ -49,17 +49,18 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { httpSession, _ := user.HTTPSession.Get(r, "wide-session") + httpSessionId := httpSession.Values["id"].(string) + // TODO: 写死以 admin 作为用户登录 + username := conf.Wide.Users[0].Name if httpSession.IsNew { - // TODO: 写死以 admin 作为用户登录 - name := conf.Wide.Users[0].Name - 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")))) diff --git a/shell/shells.go b/shell/shells.go index 8a3b6fb..936506c 100644 --- a/shell/shells.go +++ b/shell/shells.go @@ -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) diff --git a/user/sessions.go b/user/sessions.go index 78285c9..13de7b1 100644 --- a/user/sessions.go +++ b/user/sessions.go @@ -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 {