make each handlers of CRUD API optional

This commit is contained in:
yuyabe 2014-08-19 21:29:18 -07:00
parent 0330860e54
commit 30ead9600c

13
gin.go
View File

@ -212,6 +212,8 @@ type Resource interface {
ReadHandler(*Context)
UpdateHandler(*Context)
DeleteHandler(*Context)
Supported(string) bool
}
// It defines
@ -220,11 +222,22 @@ type Resource interface {
// PUT: /path/:id
// POST: /path/:id
func (group *RouterGroup) CRUD(path string, resource Resource) {
if resource.Supported("C") {
group.POST(path, resource.CreateHandler)
}
if resource.Supported("R") {
group.GET(path, resource.ReadHandler)
}
if resource.Supported("U") {
group.PUT(path+"/:id", resource.UpdateHandler)
}
if resource.Supported("D") {
group.DELETE(path+"/:id", resource.DeleteHandler)
}
}
// Static serves files from the given file system root.
// Internally a http.FileServer is used, therefore http.NotFound is used instead