little clean up
This commit is contained in:
22
resource.go
22
resource.go
@@ -30,13 +30,13 @@ type Resource struct {
|
||||
middlewares []func(http.Handler) http.Handler
|
||||
}
|
||||
|
||||
func sufixIt(str, sufix string) string {
|
||||
func suffixIt(str, suffix string) string {
|
||||
var p strings.Builder
|
||||
p.WriteString(str)
|
||||
if !strings.HasSuffix(str, "/") {
|
||||
p.WriteString("/")
|
||||
}
|
||||
p.WriteString(sufix)
|
||||
p.WriteString(suffix)
|
||||
return p.String()
|
||||
}
|
||||
|
||||
@@ -45,9 +45,9 @@ func (r *Resource) Index(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, r.pattern, h)
|
||||
}
|
||||
|
||||
// Create is GET /resource-name/new
|
||||
// New is GET /resource-name/new
|
||||
func (r *Resource) New(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, sufixIt(r.pattern, "new"), h)
|
||||
r.handlerFunc(http.MethodGet, suffixIt(r.pattern, "new"), h)
|
||||
}
|
||||
|
||||
// Create is POST /resource-name
|
||||
@@ -57,24 +57,24 @@ func (r *Resource) Create(h http.HandlerFunc) {
|
||||
|
||||
// Show is GET /resource-name/:id
|
||||
func (r *Resource) Show(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodGet, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
// Update is PUT /resource-name/:id
|
||||
func (r *Resource) Update(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPut, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodPut, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
// Update is PATCH /resource-name/:id
|
||||
// PartialUpdate is PATCH /resource-name/:id
|
||||
func (r *Resource) PartialUpdate(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPatch, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodPatch, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
func (r *Resource) Destroy(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodDelete, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodDelete, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
// HandleFunc registers the handler function for the given pattern.
|
||||
// handlerFunc registers the handler function for the given pattern.
|
||||
// If the given pattern conflicts, with one that is already registered, HandleFunc
|
||||
// panics.
|
||||
func (r *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
||||
@@ -90,7 +90,7 @@ func (r *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
||||
r.mux.Handle(path, stack(r.middlewares, h))
|
||||
}
|
||||
|
||||
// Use appends one or more middlewares onto the Router stack.
|
||||
// Use will register middleware(s) on Router stack.
|
||||
func (r *Resource) Use(middlewares ...func(http.Handler) http.Handler) {
|
||||
if r == nil {
|
||||
panic("serve: func Use was called on nil")
|
||||
|
Reference in New Issue
Block a user