-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoys.go
125 lines (109 loc) · 2.69 KB
/
toys.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
package main
import (
"github.com/kidstuff/mtoy/mgoauth"
"github.com/kidstuff/mtoy/mgosessions"
"github.com/kidstuff/toys"
"github.com/kidstuff/toys/secure/membership"
"github.com/kidstuff/toys/secure/membership/sessions"
"github.com/kidstuff/toys/view"
"github.com/nvcnvn/glog/dbctx"
"labix.org/v2/mgo"
"net/http"
"path"
"time"
)
type route struct {
pattern string
fn func(*controller)
}
type controller struct {
dbsess *mgo.Session
toys.Controller
sess sessions.Provider
auth membership.UserManager
tmpl *view.View
db *dbctx.DBContext
}
func (c *controller) View(page string, data interface{}) error {
return c.tmpl.Load(c, page, data)
}
func (c *controller) Close() {
c.dbsess.Close()
}
type handler struct {
path string
dbsess *mgo.Session
tmpl *view.View
_subRoutes []route
_defaultHandle func(*controller)
notifer membership.Notificater
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.path == r.URL.Path {
c := h.newcontroller(w, r)
h._defaultHandle(&c)
c.Close()
return
}
for _, rt := range h._subRoutes {
if match(path.Join(h.path, rt.pattern), r.URL.Path) {
c := h.newcontroller(w, r)
rt.fn(&c)
c.Close()
return
}
}
}
func (h *handler) newcontroller(w http.ResponseWriter, r *http.Request) controller {
c := controller{}
c.Init(w, r)
c.SetPath(h.path)
dbsess := h.dbsess.Clone()
c.dbsess = dbsess
//database collection (table)
database := dbsess.DB(DBNAME)
sessColl := database.C("toysSession")
userColl := database.C("toysUser")
rememberColl := database.C("toysUserRemember")
//web session
c.sess = mgosessions.NewMgoProvider(w, r, sessColl)
//web authenthicator
c.auth = mgoauth.NewMgoUserCtx(w, r, c.sess, userColl, rememberColl)
c.auth.SetNotificater(h.notifer)
//view template
c.tmpl = h.tmpl
//db context
c.db = dbctx.NewDBContext(database)
return c
}
// Handler returns a http.Handler
func Handler(path string, dbsess *mgo.Session, tmpl *view.View) *handler {
h := &handler{}
h.dbsess = dbsess
h.tmpl = tmpl
h.path = path
h.initSubRoutes()
h.notifer = membership.NewSMTPNotificater(
CONFIG.Get("smtp_email").(string),
CONFIG.Get("smtp_pass").(string),
CONFIG.Get("smtp_addr").(string),
int(CONFIG.Get("smtp_port").(float64)),
)
dbsess.DB(DBNAME).C("toysSession").EnsureIndex(mgo.Index{
Key: []string{"lastactivity"},
ExpireAfter: 7200 * time.Second,
})
dbsess.DB(DBNAME).C("toysUser").EnsureIndex(mgo.Index{
Key: []string{"email"},
Unique: true,
})
return h
}
// match is a wrapper function for path.Math
func match(pattern, name string) bool {
ok, err := path.Match(pattern, name)
if err != nil {
return false
}
return ok
}