little clean up

This commit is contained in:
Ankit Patial
2024-10-12 20:13:33 +05:30
parent 0d3181ea75
commit 8035c6f7af
3 changed files with 43 additions and 21 deletions

View File

@@ -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")