/
context.go
53 lines (41 loc) · 1.08 KB
/
context.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
package graf
import (
"encoding/gob"
"net/http"
"github.com/carbocation/go.user"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
)
const (
ThisUser = 0
)
func init() {
//Tell gob about non-standard things we'll be serializing to disk
gob.Register(new(user.User))
}
func OpenContext(req *http.Request) {
session, _ := store.Get(req, "app")
//Put the user into context
if session.Values["user"] != nil {
context.Set(req, ThisUser, session.Values["user"])
} else {
context.Set(req, ThisUser, new(user.User))
}
}
//Anything that satisfies the http.ResponseWriter interface is sufficient
func CloseContext(req *http.Request, buf http.ResponseWriter) (httpStatus int) {
session, _ := store.Get(req, "app")
err := session.Save(req, buf)
if err != nil {
return http.StatusInternalServerError
}
return http.StatusOK
}
func DeleteContext(req *http.Request, w http.ResponseWriter) {
//Delete all user-set variables
context.Purge(0)
//Destroy the session
session, _ := store.Get(req, "app")
session.Options = &sessions.Options{MaxAge: -1}
session.Save(req, w)
}