/
route.go
133 lines (111 loc) · 3.44 KB
/
route.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
package admin
import (
"log"
"net/http"
"path"
"regexp"
"strings"
"time"
"github.com/qor/qor"
"github.com/qor/qor/roles"
)
type (
Handle func(c *Context)
Handler struct {
Path *regexp.Regexp
Handle Handle
}
)
type Router struct {
Prefix string
routers map[string][]Handler
}
func newRouter() *Router {
return &Router{routers: map[string][]Handler{
"GET": []Handler{},
"PUT": []Handler{},
"POST": []Handler{},
"DELETE": []Handler{},
}}
}
func (r *Router) Get(path string, handle Handle) {
r.routers["GET"] = append(r.routers["GET"], Handler{Path: regexp.MustCompile(path), Handle: handle})
}
func (r *Router) Post(path string, handle Handle) {
r.routers["POST"] = append(r.routers["POST"], Handler{Path: regexp.MustCompile(path), Handle: handle})
}
func (r *Router) Put(path string, handle Handle) {
r.routers["PUT"] = append(r.routers["PUT"], Handler{Path: regexp.MustCompile(path), Handle: handle})
}
func (r *Router) Delete(path string, handle Handle) {
r.routers["DELETE"] = append(r.routers["DELETE"], Handler{Path: regexp.MustCompile(path), Handle: handle})
}
func (admin *Admin) MountTo(prefix string, mux *http.ServeMux) {
prefix = "/" + strings.Trim(prefix, "/")
router := admin.router
router.Prefix = prefix
controller := &controller{admin}
router.Get("^/assets/.*$", controller.Asset)
router.Get("^/?$", controller.Dashboard)
router.Get("^/[^/]+/new$", controller.New)
router.Post("^/[^/]+$", controller.Create)
router.Post("^/[^/]+/action/[^/]+(\\?.*)?$", controller.Action)
router.Get("^/[^/]+/.*$", controller.Show)
router.Put("^/[^/]+/.*$", controller.Update)
router.Post("^/[^/]+/.*$", controller.Update)
router.Delete("^/[^/]+/.*$", controller.Delete)
router.Get("^/[^/]+$", controller.Index)
mux.Handle(prefix, admin) // /:prefix
mux.Handle(prefix+"/", admin) // /:prefix/:xxx
admin.linkMenus()
}
func (admin *Admin) NewContext(w http.ResponseWriter, r *http.Request) *Context {
var currentUser qor.CurrentUser
context := Context{Context: &qor.Context{Config: admin.Config, Request: r}, Writer: w, Admin: admin}
if admin.auth != nil {
if currentUser = admin.auth.GetCurrentUser(&context); currentUser == nil {
admin.auth.Login(&context)
} else {
context.CurrentUser = currentUser
}
}
context.Roles = roles.MatchedRoles(r, currentUser)
return &context
}
var DisableLogging bool
func (admin *Admin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer func() func() {
if DisableLogging {
return func() {}
}
begin := time.Now()
log.Printf("Start [%s] %s\n", req.Method, req.RequestURI)
return func() {
log.Printf("Finish [%s] %s Took %.2fms\n", req.Method, req.RequestURI, time.Now().Sub(begin).Seconds()*1000)
}
}()()
// 128 MB
req.ParseMultipartForm(32 << 22)
if len(req.Form["_method"]) > 0 {
req.Method = strings.ToUpper(req.Form["_method"][0])
}
var router = admin.router
var context = admin.NewContext(w, req)
var pathMatch = regexp.MustCompile(path.Join(router.Prefix, `(\w+)(?:/(\w+))?[^/]*`))
var matches = pathMatch.FindStringSubmatch(req.URL.Path)
if len(matches) > 1 {
context.SetResource(admin.GetResource(matches[1]))
if len(matches) > 2 {
context.ResourceID = matches[2]
}
}
routers := router.routers[strings.ToUpper(req.Method)]
relativePath := strings.TrimPrefix(req.URL.Path, router.Prefix)
for _, handler := range routers {
if handler.Path.MatchString(relativePath) {
handler.Handle(context)
return
}
}
http.NotFound(w, req)
}