-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontroller.go
97 lines (83 loc) · 3.32 KB
/
controller.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
package frodo
import (
"net/http"
)
// BaseController defines the basic structure of a REST application controller,
// the devs controller should embed this to create their own controllers.
// It then automatically implements CRUDController, and can be passed as a controller in routing
type BaseController struct {
Attributes
}
// CRUDController can be used to parse back the developer's controller back to it's own type.
// Since we know that a REST controller entails the following methods then any struct
// that implements the Controller methods suffices the CRUDController
type CRUDController interface {
Index(http.ResponseWriter, *Request)
Create(http.ResponseWriter, *Request)
Store(http.ResponseWriter, *Request)
Show(http.ResponseWriter, *Request)
Edit(http.ResponseWriter, *Request)
Update(http.ResponseWriter, *Request)
Patch(http.ResponseWriter, *Request)
Destroy(http.ResponseWriter, *Request)
Head(http.ResponseWriter, *Request)
Options(http.ResponseWriter, *Request)
Next(...interface{})
}
// Index is the default handler for any incoming request or route's request that is not matched to it's handler
// It can also be used for specific route, mostly for the root routes("/")
func (c *BaseController) Index(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Create handles a request to Show the form for creating a new resource.
// * GET /posts/create
func (c *BaseController) Create(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Store handles a request to Store a newly created resource in storage.
// * POST /posts
func (c *BaseController) Store(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Show handles a request to Display the specified resource.
// * GET /posts/{id}
func (c *BaseController) Show(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Edit handles a request to Show the form for editing the specified resource.
// * GET /posts/{id}/edit
func (c *BaseController) Edit(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Update handles a request to update the specified resource in storage.
// * PUT /posts/{id}
func (c *BaseController) Update(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Patch is an alternative to Update
func (c *BaseController) Patch(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Destroy handles a request to Remove the specified resource from storage.
// * DELETE /posts/{id}
func (c *BaseController) Destroy(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Head handle HEAD request.
func (c *BaseController) Head(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Options handle OPTIONS request.
func (c *BaseController) Options(w http.ResponseWriter, r *Request) {
http.Error(w, "Method Not Allowed", 405)
}
// Next will be used to call the next handler in line/queue
// th biggest change is that it requires the Request struct to be passed as parametre
// to call the next handler in line
//
// it also makes the Controller implement the Middleware type
func (c *BaseController) Next(args ...interface{}) {
// We only need the 1st argument
// the Request Object
// r := args[0].(*Request)
}