From 30ead9600ccd9c9f413c2acb1b8cfc6075671afa Mon Sep 17 00:00:00 2001 From: yuyabe Date: Tue, 19 Aug 2014 21:29:18 -0700 Subject: [PATCH] make each handlers of CRUD API optional --- gin.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/gin.go b/gin.go index c8ff4744..36fdac0a 100644 --- a/gin.go +++ b/gin.go @@ -212,6 +212,8 @@ type Resource interface { ReadHandler(*Context) UpdateHandler(*Context) DeleteHandler(*Context) + + Supported(string) bool } // It defines @@ -220,10 +222,21 @@ type Resource interface { // PUT: /path/:id // POST: /path/:id func (group *RouterGroup) CRUD(path string, resource Resource) { - group.POST(path, resource.CreateHandler) - group.GET(path, resource.ReadHandler) - group.PUT(path+"/:id", resource.UpdateHandler) - group.DELETE(path+"/:id", resource.DeleteHandler) + 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.