mux/resource.go

100 lines
2.6 KiB
Go
Raw Normal View History

2024-10-12 14:04:17 +00:00
package mux
import (
"fmt"
"net/http"
"strings"
)
// Resource is a resourceful route provides a mapping between HTTP verbs and URLs and controller actions.
// By convention, each action also maps to particular CRUD operations in a database.
// A single entry in the routing file, such as
// Index route
//
// GET /resource-name # index route
//
// GET /resource-name/new # create resource page
//
// POST /resource-name # create resource post
//
// GET /resource-name/:id # view resource
//
// GET /resource-name/:id/edit # edit resource
//
// PUT /resource-name/:id # update resource
//
// DELETE /resource-name/:id # delete resource
type Resource struct {
mux *http.ServeMux
pattern string
middlewares []func(http.Handler) http.Handler
}
func sufixIt(str, sufix string) string {
var p strings.Builder
p.WriteString(str)
if !strings.HasSuffix(str, "/") {
p.WriteString("/")
}
p.WriteString(sufix)
return p.String()
}
// Index is GET /resource-name
func (r *Resource) Index(h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, r.pattern, h)
}
// Create is GET /resource-name/new
func (r *Resource) New(h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, sufixIt(r.pattern, "new"), h)
}
// Create is POST /resource-name
func (r *Resource) Create(h http.HandlerFunc) {
r.handlerFunc(http.MethodPost, r.pattern, h)
}
// Show is GET /resource-name/:id
func (r *Resource) Show(h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, sufixIt(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)
}
// Update is PATCH /resource-name/:id
func (r *Resource) PartialUpdate(h http.HandlerFunc) {
r.handlerFunc(http.MethodPatch, sufixIt(r.pattern, "{id}"), h)
}
func (r *Resource) Destroy(h http.HandlerFunc) {
r.handlerFunc(http.MethodDelete, sufixIt(r.pattern, "{id}"), h)
}
// HandleFunc 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) {
if r == nil {
panic("serve: func handlerFunc() was called on nil")
}
if r.mux == nil {
panic("serve: router mux is nil")
}
path := fmt.Sprintf("%s %s", method, pattern)
r.mux.Handle(path, stack(r.middlewares, h))
}
// Use appends one or more middlewares onto the Router stack.
func (r *Resource) Use(middlewares ...func(http.Handler) http.Handler) {
if r == nil {
panic("serve: func Use was called on nil")
}
r.middlewares = append(r.middlewares, middlewares...)
}