feat: improve resource routing API and add comprehensive quality standards

BREAKING CHANGES:
- Renamed HandleGET -> MemberGET for member-level routes
- Renamed HandlePOST -> MemberPOST for member-level routes
- Renamed HandlePUT -> MemberPUT for member-level routes
- Renamed HandlePATCH -> MemberPATCH for member-level routes
- Renamed HandleDELETE -> MemberDELETE for member-level routes

New Features:
- Added collection-level route methods: GET, POST, PUT, PATCH, DELETE
- Clear distinction between collection (/pattern/action) and member (/pattern/{id}/action) routes
- Comprehensive documentation (README, CONTRIBUTING, QUICKSTART, DOCS)
- Development tooling (Makefile, check.sh script)
- AI coding assistant guidelines (.cursorrules)
- GitHub Actions CI/CD pipeline
- golangci-lint configuration

Code Quality:
- Optimized struct field alignment for better memory usage
- All code passes go vet, staticcheck, and fieldalignment
- All tests pass with race detector
- Go 1.25+ requirement enforced

Documentation:
- Complete README rewrite with examples
- CONTRIBUTING.md with development guidelines
- QUICKSTART.md for new users
- CHANGELOG.md with version history
- SUMMARY.md documenting all changes
- DOCS.md as documentation index
This commit is contained in:
2025-11-15 14:05:11 +05:30
parent 75d2f88c65
commit 26bb9bf5ee
16 changed files with 2850 additions and 139 deletions

View File

@@ -8,19 +8,19 @@ import (
type Resource struct {
mux *http.ServeMux
routes *RouteList
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
// - 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), mw ...func(http.Handler) http.Handler) {
if m == nil {
panic("mux: Resource() called on nil")
@@ -69,7 +69,7 @@ func (res *Resource) Create(h http.HandlerFunc) {
// View a resource
//
// GET /pattern/:id
// GET /pattern/{id}
func (res *Resource) View(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodGet + " " + p)
@@ -78,7 +78,7 @@ func (res *Resource) View(h http.HandlerFunc) {
// Update a resource
//
// PUT /pattern/:id
// PUT /pattern/{id}
func (res *Resource) Update(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodPut + " " + p)
@@ -86,7 +86,7 @@ func (res *Resource) Update(h http.HandlerFunc) {
}
// UpdatePartial resource info
// PATCH /pattern/:id
// PATCH /pattern/{id}
func (res *Resource) UpdatePartial(h http.HandlerFunc) {
p := suffixIt(res.pattern, "{id}")
res.routes.Add(http.MethodPatch + " " + p)
@@ -95,39 +95,93 @@ func (res *Resource) UpdatePartial(h http.HandlerFunc) {
// Delete a resource
//
// DELETE /pattern/:id
// 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)
}
// HandleGET on /group-pattern/:id/pattern
func (res *Resource) HandleGET(pattern string, h http.HandlerFunc) {
res.handle(http.MethodGet, pattern, h)
// 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)
}
// HandlePOST on /group-pattern/:id/pattern
func (res *Resource) HandlePOST(pattern string, h http.HandlerFunc) {
res.handle(http.MethodPost, 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)
}
// HandlePUT on /group-pattern/:id/pattern
func (res *Resource) HandlePUT(pattern string, h http.HandlerFunc) {
res.handle(http.MethodPut, 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)
}
// HandlePATCH on /group-pattern/:id/pattern
func (res *Resource) HandlePATCH(pattern string, h http.HandlerFunc) {
res.handle(http.MethodPatch, pattern, h)
// 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)
}
// HandleDELETE on /group-pattern/:id/pattern
func (res *Resource) HandleDELETE(pattern string, h http.HandlerFunc) {
res.handle(http.MethodDelete, pattern, h)
// 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)
}
func (res *Resource) handle(method string, pattern string, h http.HandlerFunc) {
// 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)
}
// 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)
}
func (res *Resource) member(method string, pattern string, h http.HandlerFunc) {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}