Wide Session
This commit is contained in:
parent
5e01f1e394
commit
2fe16c646f
20
main.go
20
main.go
|
@ -49,17 +49,18 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
httpSession, _ := user.HTTPSession.Get(r, "wide-session")
|
httpSession, _ := user.HTTPSession.Get(r, "wide-session")
|
||||||
|
|
||||||
if httpSession.IsNew {
|
httpSessionId := httpSession.Values["id"].(string)
|
||||||
// TODO: 写死以 admin 作为用户登录
|
// TODO: 写死以 admin 作为用户登录
|
||||||
name := conf.Wide.Users[0].Name
|
username := conf.Wide.Users[0].Name
|
||||||
|
if httpSession.IsNew {
|
||||||
|
|
||||||
httpSession.Values["username"] = name
|
httpSession.Values["username"] = username
|
||||||
httpSessionId := strconv.Itoa(rand.Int())
|
httpSessionId = strconv.Itoa(rand.Int())
|
||||||
httpSession.Values["id"] = httpSessionId
|
httpSession.Values["id"] = httpSessionId
|
||||||
// 一天过期
|
// 一天过期
|
||||||
httpSession.Options.MaxAge = 60 * 60 * 24
|
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)
|
httpSession.Save(r, w)
|
||||||
|
@ -67,6 +68,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Wide 会话关联 HTTP 会话
|
// Wide 会话关联 HTTP 会话
|
||||||
wideSession.HTTPSessionId = httpSession.Values["id"].(string)
|
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")
|
t, err := template.ParseFiles("view/index.html")
|
||||||
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -79,6 +83,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
t.Execute(w, model)
|
t.Execute(w, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// favicon.ico 请求处理.
|
||||||
|
func faviconHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// TODO: favicon.ico 请求处理.
|
||||||
|
}
|
||||||
|
|
||||||
// 主程序入口.
|
// 主程序入口.
|
||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(conf.Wide.MaxProcs)
|
runtime.GOMAXPROCS(conf.Wide.MaxProcs)
|
||||||
|
@ -87,6 +96,7 @@ func main() {
|
||||||
|
|
||||||
// 静态资源
|
// 静态资源
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
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"))))
|
http.Handle("/data/", http.StripPrefix("/data/", http.FileServer(http.Dir("data"))))
|
||||||
|
|
|
@ -45,7 +45,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// 一天过期
|
// 一天过期
|
||||||
httpSession.Options.MaxAge = 60 * 60 * 24
|
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)
|
httpSession.Save(r, w)
|
||||||
|
|
|
@ -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 会话.
|
// 移除 HTTP 会话关联的所有 Wide 会话.
|
||||||
func (sessions *Sessions) RemoveByHTTPSid(httpSessionId string) {
|
func (sessions *Sessions) RemoveByHTTPSid(httpSessionId string) {
|
||||||
for i, s := range *sessions {
|
for i, s := range *sessions {
|
||||||
|
|
Loading…
Reference in New Issue