-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.go
93 lines (76 loc) · 2.44 KB
/
controllers.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
package gorest
import (
"fmt"
"net/http"
)
func AsListController(i interface{}) ListController {
switch i := i.(type) {
case http.Handler:
return httpHandlerAsListController{Handler: i}
case http.HandlerFunc:
return httpHandlerAsListController{Handler: i}
default:
panic(fmt.Sprintf(`unknown type: %T`, i))
}
}
func AsCreateController(i interface{}) CreateController {
switch i := i.(type) {
case http.Handler:
return httpHandlerAsCreateController{Handler: i}
case http.HandlerFunc:
return httpHandlerAsCreateController{Handler: i}
default:
panic(fmt.Sprintf(`unknown type: %T`, i))
}
}
func AsShowController(i interface{}) ShowController {
switch i := i.(type) {
case http.Handler:
return httpHandlerAsShowController{Handler: i}
case http.HandlerFunc:
return httpHandlerAsShowController{Handler: i}
default:
panic(fmt.Sprintf(`unknown type: %T`, i))
}
}
func AsUpdateController(i interface{}) UpdateController {
switch i := i.(type) {
case http.Handler:
return httpHandlerAsUpdateController{Handler: i}
case http.HandlerFunc:
return httpHandlerAsUpdateController{Handler: i}
default:
panic(fmt.Sprintf(`unknown type: %T`, i))
}
}
func AsDeleteController(i interface{}) DeleteController {
switch i := i.(type) {
case http.Handler:
return httpHandlerAsDeleteController{Handler: i}
case http.HandlerFunc:
return httpHandlerAsDeleteController{Handler: i}
default:
panic(fmt.Sprintf(`unknown type: %T`, i))
}
}
//--------------------------------------------------- http.Handler ---------------------------------------------------//
type httpHandlerAsListController struct{ http.Handler }
func (ctrl httpHandlerAsListController) List(w http.ResponseWriter, r *http.Request) {
ctrl.Handler.ServeHTTP(w, r)
}
type httpHandlerAsCreateController struct{ http.Handler }
func (ctrl httpHandlerAsCreateController) Create(w http.ResponseWriter, r *http.Request) {
ctrl.Handler.ServeHTTP(w, r)
}
type httpHandlerAsShowController struct{ http.Handler }
func (ctrl httpHandlerAsShowController) Show(w http.ResponseWriter, r *http.Request) {
ctrl.Handler.ServeHTTP(w, r)
}
type httpHandlerAsUpdateController struct{ http.Handler }
func (ctrl httpHandlerAsUpdateController) Update(w http.ResponseWriter, r *http.Request) {
ctrl.Handler.ServeHTTP(w, r)
}
type httpHandlerAsDeleteController struct{ http.Handler }
func (ctrl httpHandlerAsDeleteController) Delete(w http.ResponseWriter, r *http.Request) {
ctrl.Handler.ServeHTTP(w, r)
}