Split code in respective files.

Resource method name change.

Route list func
This commit is contained in:
2025-08-16 11:19:45 +05:30
parent f91090b35e
commit 855b82e9df
10 changed files with 506 additions and 366 deletions

View File

@@ -6,28 +6,134 @@ import (
"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
routes *RouteList
}
// 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
func (m *Mux) Resource(pattern string, fn func(res *Resource)) {
if m == nil {
panic("mux: Resource() called on nil")
}
if strings.TrimSpace(pattern) == "" {
panic("mux: Resource() requires a patter to work")
}
if fn == nil {
panic("mux: Resource() requires callback")
}
// Copy root middlewares.
mws := make([]func(http.Handler) http.Handler, len(m.middlewares))
copy(mws, m.middlewares)
fn(&Resource{
mux: m.mux,
pattern: pattern,
middlewares: mws,
routes: m.routes,
})
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
// 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)
}
func (res *Resource) Handle(pattern string, h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodDelete + " " + p)
res.handlerFunc(http.MethodDelete, p, h)
}
// handlerFunc registers the handler function for the given pattern.
// 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 {
panic("serve: func handlerFunc() was called on nil")
}
if res.mux == nil {
panic("serve: router mux is nil")
}
path := fmt.Sprintf("%s %s", method, pattern)
res.mux.Handle(path, stack(res.middlewares, h))
}
// Use will register middleware(s) on Router stack.
func (res *Resource) Use(middlewares ...func(http.Handler) http.Handler) {
if res == nil {
panic("serve: func Use was called on nil")
}
res.middlewares = append(res.middlewares, middlewares...)
}
func suffixIt(str, suffix string) string {
@@ -39,61 +145,3 @@ func suffixIt(str, suffix string) string {
p.WriteString(suffix)
return p.String()
}
// Index is GET /resource-name
func (r *Resource) Index(h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, r.pattern, h)
}
// New is GET /resource-name/new
func (r *Resource) New(h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, suffixIt(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, suffixIt(r.pattern, "{id}"), h)
}
// Update is PUT /resource-name/:id
func (r *Resource) Update(h http.HandlerFunc) {
r.handlerFunc(http.MethodPut, suffixIt(r.pattern, "{id}"), h)
}
// PartialUpdate is PATCH /resource-name/:id
func (r *Resource) PartialUpdate(h http.HandlerFunc) {
r.handlerFunc(http.MethodPatch, suffixIt(r.pattern, "{id}"), h)
}
func (r *Resource) Destroy(h http.HandlerFunc) {
r.handlerFunc(http.MethodDelete, suffixIt(r.pattern, "{id}"), h)
}
// 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) {
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 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")
}
r.middlewares = append(r.middlewares, middlewares...)
}