Files
mux/resource.go

226 lines
5.9 KiB
Go
Raw Normal View History

2024-10-12 19:34:17 +05:30
package mux
import (
"fmt"
"net/http"
"strings"
)
type Resource struct {
mux *http.ServeMux
routes *RouteList
2024-10-12 19:34:17 +05:30
pattern string
middlewares []func(http.Handler) http.Handler
}
// Resource routes mapping by using HTTP verbs
// - GET /pattern view all resources
// - GET /pattern/create new resource view
// - POST /pattern create a new resource
// - GET /pattern/{id} view a resource
// - PUT /pattern/{id} update a resource
// - PATCH /pattern/{id} partial update a resource
// - DELETE /resource/{id} delete a resource
2025-08-16 14:43:41 +05:30
func (m *Mux) Resource(pattern string, fn func(res *Resource), mw ...func(http.Handler) http.Handler) {
if m == nil {
panic("mux: Resource() called on nil")
2024-10-12 19:34:17 +05:30
}
if strings.TrimSpace(pattern) == "" {
panic("mux: Resource() requires a patter to work")
}
if fn == nil {
panic("mux: Resource() requires callback")
}
fn(&Resource{
mux: m.mux,
pattern: pattern,
2025-08-16 19:25:00 +05:30
middlewares: copyMW(m.middlewares, mw),
routes: m.routes,
})
2024-10-12 19:34:17 +05:30
}
// Index of all resource.
//
// GET /pattern
func (res *Resource) Index(h http.HandlerFunc) {
res.routes.Add(http.MethodGet + " " + res.pattern)
res.handlerFunc(http.MethodGet, res.pattern, h)
}
// CreateView new resource
//
// GET /pattern/create
func (res *Resource) CreateView(h http.HandlerFunc) {
p := suffixIt(res.pattern, "create")
res.routes.Add(http.MethodGet + " " + p)
res.handlerFunc(http.MethodGet, p, h)
2024-10-12 19:34:17 +05:30
}
// Create a new resource
//
// POST /pattern/create
func (res *Resource) Create(h http.HandlerFunc) {
res.routes.Add(http.MethodPost + " " + res.pattern)
res.handlerFunc(http.MethodPost, res.pattern, h)
2024-10-12 19:34:17 +05:30
}
// View a resource
//
// GET /pattern/{id}
func (res *Resource) View(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodGet + " " + p)
res.handlerFunc(http.MethodGet, p, h)
2024-10-12 19:34:17 +05:30
}
// Update a resource
//
// PUT /pattern/{id}
func (res *Resource) Update(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodPut + " " + p)
res.handlerFunc(http.MethodPut, p, h)
2024-10-12 19:34:17 +05:30
}
// UpdatePartial resource info
// PATCH /pattern/{id}
func (res *Resource) UpdatePartial(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodPatch + " " + p)
res.handlerFunc(http.MethodPatch, p, h)
2024-10-12 19:34:17 +05:30
}
// Delete a resource
//
// DELETE /pattern/{id}
func (res *Resource) Delete(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodDelete + " " + p)
res.handlerFunc(http.MethodDelete, p, h)
2024-10-12 19:34:17 +05:30
}
// GET registers a custom GET route at collection level
//
// GET /pattern/route
func (res *Resource) GET(pattern string, h http.HandlerFunc) {
res.collection(http.MethodGet, pattern, h)
}
// POST registers a custom POST route at collection level
//
// POST /pattern/route
func (res *Resource) POST(pattern string, h http.HandlerFunc) {
res.collection(http.MethodPost, pattern, h)
}
// PUT registers a custom PUT route at collection level
//
// PUT /pattern/route
func (res *Resource) PUT(pattern string, h http.HandlerFunc) {
res.collection(http.MethodPut, pattern, h)
2025-08-17 20:04:48 +05:30
}
// PATCH registers a custom PATCH route at collection level
//
// PATCH /pattern/route
func (res *Resource) PATCH(pattern string, h http.HandlerFunc) {
res.collection(http.MethodPatch, pattern, h)
2025-08-17 20:04:48 +05:30
}
// DELETE registers a custom DELETE route at collection level
//
// DELETE /pattern/route
func (res *Resource) DELETE(pattern string, h http.HandlerFunc) {
res.collection(http.MethodDelete, pattern, h)
2025-08-17 20:04:48 +05:30
}
// MemberGET registers a custom GET route at member level
//
// GET /pattern/{id}/route
func (res *Resource) MemberGET(pattern string, h http.HandlerFunc) {
res.member(http.MethodGet, pattern, h)
2025-08-17 20:04:48 +05:30
}
// MemberPOST registers a custom POST route at member level
//
// POST /pattern/{id}/route
func (res *Resource) MemberPOST(pattern string, h http.HandlerFunc) {
res.member(http.MethodPost, pattern, h)
}
// MemberPUT registers a custom PUT route at member level
//
// PUT /pattern/{id}/route
func (res *Resource) MemberPUT(pattern string, h http.HandlerFunc) {
res.member(http.MethodPut, pattern, h)
}
// MemberPATCH registers a custom PATCH route at member level
//
// PATCH /pattern/{id}/route
func (res *Resource) MemberPATCH(pattern string, h http.HandlerFunc) {
res.member(http.MethodPatch, pattern, h)
}
// MemberDELETE registers a custom DELETE route at member level
//
// DELETE /pattern/{id}/route
func (res *Resource) MemberDELETE(pattern string, h http.HandlerFunc) {
res.member(http.MethodDelete, pattern, h)
}
func (res *Resource) collection(method string, pattern string, h http.HandlerFunc) {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
p := suffixIt(res.pattern, pattern)
res.routes.Add(method + " " + p)
res.handlerFunc(method, p, h)
2025-08-17 20:04:48 +05:30
}
func (res *Resource) member(method string, pattern string, h http.HandlerFunc) {
2025-08-17 20:04:48 +05:30
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
p := suffixIt(res.pattern, "{id}"+pattern)
res.routes.Add(method + " " + p)
res.handlerFunc(method, p, h)
2024-10-12 19:34:17 +05:30
}
2024-10-12 20:13:33 +05:30
// handlerFunc registers the handler function for the given pattern.
2024-10-12 19:34:17 +05:30
// If the given pattern conflicts, with one that is already registered, HandleFunc
// panics.
func (res *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
if res == nil {
2024-10-12 19:34:17 +05:30
panic("serve: func handlerFunc() was called on nil")
}
if res.mux == nil {
2024-10-12 19:34:17 +05:30
panic("serve: router mux is nil")
}
path := fmt.Sprintf("%s %s", method, pattern)
2025-08-16 19:25:00 +05:30
res.mux.Handle(path, stack(h, res.middlewares))
2024-10-12 19:34:17 +05:30
}
2024-10-12 20:13:33 +05:30
// Use will register middleware(s) on Router stack.
func (res *Resource) Use(middlewares ...func(http.Handler) http.Handler) {
if res == nil {
2024-10-12 19:34:17 +05:30
panic("serve: func Use was called on nil")
}
res.middlewares = append(res.middlewares, middlewares...)
}
func suffixIt(str, suffix string) string {
var p strings.Builder
p.WriteString(str)
if !strings.HasSuffix(str, "/") {
p.WriteString("/")
}
p.WriteString(suffix)
return p.String()
2024-10-12 19:34:17 +05:30
}