-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoys.go
104 lines (83 loc) · 2.07 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
package gorms
import (
"github.com/nvcnvn/gorms/dbctx"
"github.com/openvn/toys"
"github.com/openvn/toys/secure/membership"
"github.com/openvn/toys/secure/membership/session"
"github.com/openvn/toys/view"
"labix.org/v2/mgo"
"net/http"
"path"
"time"
)
const (
dbname string = "test"
)
type Controller struct {
toys.Controller
sess session.Provider
auth membership.Authenticater
tmpl *view.View
db *dbctx.DBCtx
}
func (c *Controller) NewViewData(title string) map[string]interface{} {
m := make(map[string]interface{})
m["Title"] = title
m["DBCtx"] = c.db
return m
}
func (c *Controller) View(page string, data interface{}) {
c.tmpl.Load(c, page, data)
}
type Handler struct {
fn func(c *Controller)
dbsess *mgo.Session
tmpl *view.View
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := Controller{}
c.Init(w, r)
dbsess := h.dbsess.Clone()
defer dbsess.Close()
//database collection (table)
database := dbsess.DB(dbname)
sessColl := database.C("toysSession")
userColl := database.C("toysUser")
rememberColl := database.C("toysUserRemember")
houseColl := database.C("toysOrgs")
personColl := database.C("toysPerson")
//web session
c.sess = session.NewMgoProvider(w, r, sessColl)
//web authenthicator
c.auth = membership.NewAuthDBCtx(w, r, c.sess, userColl, rememberColl)
//database context
c.db = dbctx.NewDBCtx(houseColl, personColl)
//view template
c.tmpl = h.tmpl
//process
h.fn(&c)
}
// NewHandler receive a controll function and a mongodb session
func NewHandler(f func(c *Controller), dbsess *mgo.Session, tmpl *view.View) *Handler {
h := &Handler{}
h.dbsess = dbsess
h.tmpl = tmpl
h.fn = f
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
}