little clean up
This commit is contained in:
parent
0d3181ea75
commit
8035c6f7af
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,3 +21,5 @@
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# GoLand
|
||||
.idea
|
||||
|
22
resource.go
22
resource.go
@ -30,13 +30,13 @@ type Resource struct {
|
||||
middlewares []func(http.Handler) http.Handler
|
||||
}
|
||||
|
||||
func sufixIt(str, sufix string) string {
|
||||
func suffixIt(str, suffix string) string {
|
||||
var p strings.Builder
|
||||
p.WriteString(str)
|
||||
if !strings.HasSuffix(str, "/") {
|
||||
p.WriteString("/")
|
||||
}
|
||||
p.WriteString(sufix)
|
||||
p.WriteString(suffix)
|
||||
return p.String()
|
||||
}
|
||||
|
||||
@ -45,9 +45,9 @@ func (r *Resource) Index(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, r.pattern, h)
|
||||
}
|
||||
|
||||
// Create is GET /resource-name/new
|
||||
// New is GET /resource-name/new
|
||||
func (r *Resource) New(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, sufixIt(r.pattern, "new"), h)
|
||||
r.handlerFunc(http.MethodGet, suffixIt(r.pattern, "new"), h)
|
||||
}
|
||||
|
||||
// Create is POST /resource-name
|
||||
@ -57,24 +57,24 @@ func (r *Resource) Create(h http.HandlerFunc) {
|
||||
|
||||
// Show is GET /resource-name/:id
|
||||
func (r *Resource) Show(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, sufixIt(r.pattern, "{id}"), h)
|
||||
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, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodPut, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
// Update is PATCH /resource-name/:id
|
||||
// PartialUpdate is PATCH /resource-name/:id
|
||||
func (r *Resource) PartialUpdate(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPatch, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodPatch, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
func (r *Resource) Destroy(h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodDelete, sufixIt(r.pattern, "{id}"), h)
|
||||
r.handlerFunc(http.MethodDelete, suffixIt(r.pattern, "{id}"), h)
|
||||
}
|
||||
|
||||
// HandleFunc registers the handler function for the given pattern.
|
||||
// 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) {
|
||||
@ -90,7 +90,7 @@ func (r *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
||||
r.mux.Handle(path, stack(r.middlewares, h))
|
||||
}
|
||||
|
||||
// Use appends one or more middlewares onto the Router stack.
|
||||
// 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")
|
||||
|
40
router.go
40
router.go
@ -3,27 +3,27 @@ package mux
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const RouteCtxKey = "ServeCTX"
|
||||
|
||||
type (
|
||||
// Router is a wrapper arround the go's standard http.ServeMux.
|
||||
// Its a lean wrapper with feature that are there to make easy use of it.
|
||||
// Router is a wrapper around the go's standard http.ServeMux.
|
||||
// It's a lean wrapper with methods to make routing easier
|
||||
Router struct {
|
||||
mux *http.ServeMux
|
||||
middlewares []func(http.Handler) http.Handler
|
||||
}
|
||||
)
|
||||
|
||||
// New instance of Mux
|
||||
func NewRouter() *Router {
|
||||
return &Router{
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
}
|
||||
|
||||
// Use appends one or more middlewares onto the Router stack.
|
||||
// Use will register middleware(s) with router stack
|
||||
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||
if r == nil {
|
||||
panic("serve: func Use was called on nil")
|
||||
@ -31,38 +31,47 @@ func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||
r.middlewares = append(r.middlewares, h...)
|
||||
}
|
||||
|
||||
// Get method route
|
||||
func (r *Router) Get(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, pattern, h)
|
||||
}
|
||||
|
||||
// Head method route
|
||||
func (r *Router) Head(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodHead, pattern, h)
|
||||
}
|
||||
|
||||
// Post method route
|
||||
func (r *Router) Post(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPost, pattern, h)
|
||||
}
|
||||
|
||||
// Put method route
|
||||
func (r *Router) Put(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPut, pattern, h)
|
||||
}
|
||||
|
||||
// Patch method route
|
||||
func (r *Router) Patch(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPatch, pattern, h)
|
||||
}
|
||||
|
||||
// Delete method route
|
||||
func (r *Router) Delete(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodDelete, pattern, h)
|
||||
}
|
||||
|
||||
// Connect method route
|
||||
func (r *Router) Connect(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodConnect, pattern, h)
|
||||
}
|
||||
|
||||
// Options method route
|
||||
func (r *Router) Options(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodOptions, pattern, h)
|
||||
}
|
||||
|
||||
// Trace method route
|
||||
func (r *Router) Trace(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodTrace, pattern, h)
|
||||
}
|
||||
@ -96,27 +105,38 @@ func (r *Router) With(middleware ...func(http.Handler) http.Handler) *Router {
|
||||
// Group adds a new inline-Router along the current routing
|
||||
// path, with a fresh middleware stack for the inline-Router.
|
||||
func (r *Router) Group(fn func(grp *Router)) {
|
||||
if r == nil {
|
||||
panic("mux: Resource() called on nil")
|
||||
}
|
||||
|
||||
if fn == nil {
|
||||
return
|
||||
panic("mux: Group() requires callback")
|
||||
}
|
||||
|
||||
im := r.With()
|
||||
fn(im)
|
||||
}
|
||||
|
||||
// Route mounts a sub-Router along a `pattern“ string.
|
||||
// Resource resourceful route provides a mapping between HTTP verbs for given the pattern
|
||||
func (r *Router) Resource(pattern string, fn func(resource *Resource)) {
|
||||
if r == nil {
|
||||
panic(fmt.Sprintf("chi: attempting to Route() a nil subrouter on '%s'", pattern))
|
||||
panic("mux: Resource() called on nil")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(pattern) == "" {
|
||||
panic("mux: Resource() requires a patter to work")
|
||||
}
|
||||
|
||||
if fn == nil {
|
||||
panic(fmt.Sprintf("chi: attempting to Resource() a nil subrouter on '%s'", pattern))
|
||||
panic("mux: Resource() requires callback")
|
||||
}
|
||||
|
||||
mws := make([]func(http.Handler) http.Handler, len(r.middlewares))
|
||||
copy(mws, r.middlewares)
|
||||
fn(&Resource{
|
||||
mux: r.mux,
|
||||
pattern: pattern,
|
||||
mux: r.mux,
|
||||
pattern: pattern,
|
||||
middlewares: mws,
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user