-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.go
More file actions
41 lines (30 loc) · 957 Bytes
/
resource.go
File metadata and controls
41 lines (30 loc) · 957 Bytes
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
package ctx
import "net/http"
type Getter interface {
GET(c *Context, rw http.ResponseWriter, req *http.Request) error
}
type Poster interface {
POST(c *Context, rw http.ResponseWriter, req *http.Request) error
}
type Putter interface {
PUT(c *Context, rw http.ResponseWriter, req *http.Request) error
}
type Deleter interface {
DELETE(c *Context, rw http.ResponseWriter, req *http.Request) error
}
func Resource(path string, res interface{}, public bool) {
handlers := make(map[string]ContextHandler, 0)
if handler, implements := res.(Getter); implements {
handlers["GET"] = handler.GET
}
if handler, implements := res.(Poster); implements {
handlers["POST"] = handler.POST
}
if handler, implements := res.(Putter); implements {
handlers["PUT"] = handler.PUT
}
if handler, implements := res.(Deleter); implements {
handlers["DELETE"] = handler.DELETE
}
Register(&Endpoint{Path: path, Public: public, Handlers: handlers})
}