-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
86 lines (74 loc) · 2.63 KB
/
router.go
File metadata and controls
86 lines (74 loc) · 2.63 KB
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
package nutty
import (
"net/http"
"strings"
)
type ControllerWithIndex interface {
Index(*App, http.ResponseWriter, *http.Request)
}
type ControllerWithCreate interface {
Create(*App, http.ResponseWriter, *http.Request)
}
type ControllerWithDestroy interface {
Destroy(*App, http.ResponseWriter, *http.Request)
}
type ControllerWithUpdate interface {
Update(*App, http.ResponseWriter, *http.Request)
}
type Router struct {
handlers map[string](map[string]interface{})
initializations map[string]bool
}
// Required to use r.MultipartReader()
func (routes *Router) RawPost(uri string, controller interface{}, nuttyApp *App) {
if !routes.initializations[uri] {
routes.initializations[uri] = true
routes.handlers[uri] = make(map[string]interface{})
http.HandleFunc(uri, func(resp http.ResponseWriter, req *http.Request) {
if routes.handlers[uri][req.Method] == nil {
http.NotFound(resp, req)
} else {
(routes.handlers[uri][req.Method]).(ControllerWithCreate).Create(nuttyApp, resp, req)
}
})
}
routes.handlers[uri]["POST"] = controller
}
func (routes *Router) Map(uri string, controller interface{}, httpMethods []string, nuttyApp *App) {
if !routes.initializations[uri] {
routes.initializations[uri] = true
routes.handlers[uri] = make(map[string]interface{})
http.HandleFunc(uri, func(resp http.ResponseWriter, req *http.Request) {
if routes.handlers[uri][req.Method] == nil {
http.NotFound(resp, req)
} else {
if req.Method == "POST" {
if req.FormValue("_method") == "delete" || req.FormValue("_method") == "DELETE" {
(routes.handlers[uri][req.Method]).(ControllerWithDestroy).Destroy(nuttyApp, resp, req)
} else if req.FormValue("_method") == "put" || req.FormValue("_method") == "PUT" {
(routes.handlers[uri][req.Method]).(ControllerWithUpdate).Update(nuttyApp, resp, req)
} else {
(routes.handlers[uri][req.Method]).(ControllerWithCreate).Create(nuttyApp, resp, req)
}
} else {
(routes.handlers[uri][req.Method]).(ControllerWithIndex).Index(nuttyApp, resp, req)
}
}
})
}
for _, method := range httpMethods {
routes.handlers[uri][strings.ToUpper(method)] = controller
}
}
// Defaults to GET if no http methods sent
func (routes *Router) Root(ctrl ControllerWithIndex, nuttyApp *App) {
if !routes.initializations["/"] {
routes.initializations["/"] = true
routes.handlers["/"] = make(map[string]interface{})
}
handler := func(w http.ResponseWriter, r *http.Request) { ctrl.Index(nuttyApp, w, r) }
routes.handlers["/"]["GET"] = handler
http.HandleFunc("/", handler)
http.HandleFunc("/index", handler)
http.HandleFunc("/index.html", handler)
}