forked from TheOnly92/bloody.go
/
session.go
134 lines (120 loc) · 2.75 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package session
import (
"crypto/sha1"
"http"
"time"
"hash"
"web"
"launchpad.net/mgo"
"launchpad.net/gobson/bson"
"encoding/hex"
"json"
"strconv"
)
type Session struct {
id string
old_id string
Data map[string]interface{}
handler *MHandler
}
func Start(ctx *web.Context, handler *MHandler) *Session {
session := new(Session)
session.handler = handler
session.handler.Clean()
old := false
if ctx.Cookies != nil {
if id, exists := ctx.Cookies["bloody_sess"]; exists {
session.id = id
old = true
}
}
if !old {
// Starts new session
session.generateId()
session.handler.Store(session.GetID(), nil)
}
rt := session.handler.Retrieve(session.GetID())
json.Unmarshal(rt.SessionData, &session.Data)
if session.Data == nil {
t := make(map[string]interface{})
session.Data = t
}
ctx.SetCookie("bloody_sess", session.GetID(), time.LocalTime().Seconds() + 3600)
return session
}
func (session *Session) Save() {
session.handler.Store(session.id, session.Data)
}
func (session *Session) generateId() string {
var header = make(http.Header)
remoteAddr := header.Get("REMOTE_ADDR")
t := time.LocalTime()
var h hash.Hash = sha1.New()
h.Write([]byte(remoteAddr+strconv.Itoa64(t.Seconds())))
session.id = hex.EncodeToString(h.Sum())
return session.id
}
func (session *Session) GetID() string {
return session.id
}
func (session *Session) SetID(id string) {
session.id = id
}
type MHandler struct {
session *mgo.Session
}
type sessionRow struct {
SessionID string
ExpirationTS int64
SessionData []byte
}
func (handler *MHandler) Store(id string, data map[string]interface{}) {
c := handler.session.DB("bloody").C("sessions")
t := time.LocalTime()
b, _ := json.Marshal(data)
var sav sessionRow
sav.SessionID = id
sav.ExpirationTS = t.Seconds()+1440
sav.SessionData = b
err := c.Update(bson.M{"sessionid": id}, sav)
if err != nil {
if err == mgo.NotFound {
err = c.Insert(&sessionRow{id, t.Seconds()+1440, b})
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
}
func (handler *MHandler) Clean() {
c := handler.session.DB("bloody").C("sessions")
t := time.LocalTime()
err := c.RemoveAll(bson.M{"expirationts": bson.M{"$lt":t.Seconds()}})
if err != nil {
panic(err)
}
}
func (handler *MHandler) Remove(id string) {
c := handler.session.DB("bloody").C("sessions")
err := c.Remove(bson.M{"sessionid": id})
if err != nil {
panic(err)
}
}
func (handler *MHandler) Retrieve(id string) *sessionRow {
var rt sessionRow
c := handler.session.DB("bloody").C("sessions")
err := c.Find(bson.M{"sessionid": id}).One(&rt)
if err != nil {
if err == mgo.NotFound {
return &sessionRow{id,0,nil}
}
panic(err)
}
return &rt
}
func (handler *MHandler) SetSession(mSession *mgo.Session) {
handler.session = mSession
}