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
|
|
|
|
}
|
|
|
|
|
2024-10-12 14:43:33 +00:00
|
|
|
func suffixIt(str, suffix string) string {
|
2024-10-12 14:04:17 +00:00
|
|
|
var p strings.Builder
|
|
|
|
p.WriteString(str)
|
|
|
|
if !strings.HasSuffix(str, "/") {
|
|
|
|
p.WriteString("/")
|
|
|
|
}
|
2024-10-12 14:43:33 +00:00
|
|
|
p.WriteString(suffix)
|
2024-10-12 14:04:17 +00:00
|
|
|
return p.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index is GET /resource-name
|
|
|
|
func (r *Resource) Index(h http.HandlerFunc) {
|
|
|
|
r.handlerFunc(http.MethodGet, r.pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-10-12 14:43:33 +00:00
|
|
|
// New is GET /resource-name/new
|
2024-10-12 14:04:17 +00:00
|
|
|
func (r *Resource) New(h http.HandlerFunc) {
|
2024-10-12 14:43:33 +00:00
|
|
|
r.handlerFunc(http.MethodGet, suffixIt(r.pattern, "new"), h)
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2024-10-12 14:43:33 +00:00
|
|
|
r.handlerFunc(http.MethodGet, suffixIt(r.pattern, "{id}"), h)
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is PUT /resource-name/:id
|
|
|
|
func (r *Resource) Update(h http.HandlerFunc) {
|
2024-10-12 14:43:33 +00:00
|
|
|
r.handlerFunc(http.MethodPut, suffixIt(r.pattern, "{id}"), h)
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 14:43:33 +00:00
|
|
|
// PartialUpdate is PATCH /resource-name/:id
|
2024-10-12 14:04:17 +00:00
|
|
|
func (r *Resource) PartialUpdate(h http.HandlerFunc) {
|
2024-10-12 14:43:33 +00:00
|
|
|
r.handlerFunc(http.MethodPatch, suffixIt(r.pattern, "{id}"), h)
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource) Destroy(h http.HandlerFunc) {
|
2024-10-12 14:43:33 +00:00
|
|
|
r.handlerFunc(http.MethodDelete, suffixIt(r.pattern, "{id}"), h)
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 14:43:33 +00:00
|
|
|
// handlerFunc registers the handler function for the given pattern.
|
2024-10-12 14:04:17 +00:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2024-10-12 14:43:33 +00:00
|
|
|
// Use will register middleware(s) on Router stack.
|
2024-10-12 14:04:17 +00:00
|
|
|
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...)
|
|
|
|
}
|