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
|
package main
import (
"github.com/gorilla/sessions"
"github.com/kch42/mailremind/model"
"html/template"
"log"
"net/http"
)
type Handler func(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User)
func getSess(req *http.Request) (*sessions.Session, error) {
sess, err := SessionStorage.Get(req, "mailremind-sess")
if err != nil {
sess, err = SessionStorage.New(req, "mailremind-sess")
}
return sess, err
}
func userFromSess(sess *sessions.Session) model.User {
_id, ok := sess.Values["uid"]
if !ok {
return nil
}
id, ok := _id.(string)
if !ok {
return nil
}
uid, err := db.ParseDBID(id)
if err != nil {
return nil
}
user, err := dbcon.UserByID(uid)
if err != nil {
return nil
}
return user
}
type tpldata struct {
Mail string
Data interface{}
}
func mkHttpHandler(h Handler, tpl *template.Template) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
sess, err := getSess(req)
if err != nil {
req.Cookies()
log.Printf("Error while getting session: %s", err)
rw.Write([]byte("Unable to create a session. Try reloading the page or delete the cookies for this site."))
}
user := userFromSess(sess)
outdata, user := h(user, sess, req)
mail := ""
if user != nil {
mail = user.Email()
}
if err := sess.Save(req, rw); err != nil {
log.Printf("Error while saving session: %s", err)
}
if err := tpl.Execute(rw, &tpldata{mail, outdata}); err != nil {
log.Printf("Error executing template %s: %s", tpl.Name(), err)
}
}
}
|