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 workspace file
|
||||||
go.work
|
go.work
|
||||||
|
|
||||||
|
# GoLand
|
||||||
|
.idea
|
||||||
|
22
resource.go
22
resource.go
@ -30,13 +30,13 @@ type Resource struct {
|
|||||||
middlewares []func(http.Handler) http.Handler
|
middlewares []func(http.Handler) http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func sufixIt(str, sufix string) string {
|
func suffixIt(str, suffix string) string {
|
||||||
var p strings.Builder
|
var p strings.Builder
|
||||||
p.WriteString(str)
|
p.WriteString(str)
|
||||||
if !strings.HasSuffix(str, "/") {
|
if !strings.HasSuffix(str, "/") {
|
||||||
p.WriteString("/")
|
p.WriteString("/")
|
||||||
}
|
}
|
||||||
p.WriteString(sufix)
|
p.WriteString(suffix)
|
||||||
return p.String()
|
return p.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +45,9 @@ func (r *Resource) Index(h http.HandlerFunc) {
|
|||||||
r.handlerFunc(http.MethodGet, r.pattern, h)
|
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) {
|
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
|
// Create is POST /resource-name
|
||||||
@ -57,24 +57,24 @@ func (r *Resource) Create(h http.HandlerFunc) {
|
|||||||
|
|
||||||
// Show is GET /resource-name/:id
|
// Show is GET /resource-name/:id
|
||||||
func (r *Resource) Show(h http.HandlerFunc) {
|
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
|
// Update is PUT /resource-name/:id
|
||||||
func (r *Resource) Update(h http.HandlerFunc) {
|
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) {
|
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) {
|
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
|
// If the given pattern conflicts, with one that is already registered, HandleFunc
|
||||||
// panics.
|
// panics.
|
||||||
func (r *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
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))
|
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) {
|
func (r *Resource) Use(middlewares ...func(http.Handler) http.Handler) {
|
||||||
if r == nil {
|
if r == nil {
|
||||||
panic("serve: func Use was called on nil")
|
panic("serve: func Use was called on nil")
|
||||||
|
40
router.go
40
router.go
@ -3,27 +3,27 @@ package mux
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const RouteCtxKey = "ServeCTX"
|
const RouteCtxKey = "ServeCTX"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Router is a wrapper arround the go's standard http.ServeMux.
|
// Router is a wrapper around the go's standard http.ServeMux.
|
||||||
// Its a lean wrapper with feature that are there to make easy use of it.
|
// It's a lean wrapper with methods to make routing easier
|
||||||
Router struct {
|
Router struct {
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
middlewares []func(http.Handler) http.Handler
|
middlewares []func(http.Handler) http.Handler
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// New instance of Mux
|
|
||||||
func NewRouter() *Router {
|
func NewRouter() *Router {
|
||||||
return &Router{
|
return &Router{
|
||||||
mux: http.NewServeMux(),
|
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) {
|
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||||
if r == nil {
|
if r == nil {
|
||||||
panic("serve: func Use was called on 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...)
|
r.middlewares = append(r.middlewares, h...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get method route
|
||||||
func (r *Router) Get(pattern string, h http.HandlerFunc) {
|
func (r *Router) Get(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodGet, pattern, h)
|
r.handlerFunc(http.MethodGet, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Head method route
|
||||||
func (r *Router) Head(pattern string, h http.HandlerFunc) {
|
func (r *Router) Head(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodHead, pattern, h)
|
r.handlerFunc(http.MethodHead, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Post method route
|
||||||
func (r *Router) Post(pattern string, h http.HandlerFunc) {
|
func (r *Router) Post(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodPost, pattern, h)
|
r.handlerFunc(http.MethodPost, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put method route
|
||||||
func (r *Router) Put(pattern string, h http.HandlerFunc) {
|
func (r *Router) Put(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodPut, pattern, h)
|
r.handlerFunc(http.MethodPut, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Patch method route
|
||||||
func (r *Router) Patch(pattern string, h http.HandlerFunc) {
|
func (r *Router) Patch(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodPatch, pattern, h)
|
r.handlerFunc(http.MethodPatch, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete method route
|
||||||
func (r *Router) Delete(pattern string, h http.HandlerFunc) {
|
func (r *Router) Delete(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodDelete, pattern, h)
|
r.handlerFunc(http.MethodDelete, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect method route
|
||||||
func (r *Router) Connect(pattern string, h http.HandlerFunc) {
|
func (r *Router) Connect(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodConnect, pattern, h)
|
r.handlerFunc(http.MethodConnect, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Options method route
|
||||||
func (r *Router) Options(pattern string, h http.HandlerFunc) {
|
func (r *Router) Options(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodOptions, pattern, h)
|
r.handlerFunc(http.MethodOptions, pattern, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trace method route
|
||||||
func (r *Router) Trace(pattern string, h http.HandlerFunc) {
|
func (r *Router) Trace(pattern string, h http.HandlerFunc) {
|
||||||
r.handlerFunc(http.MethodTrace, pattern, h)
|
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
|
// Group adds a new inline-Router along the current routing
|
||||||
// path, with a fresh middleware stack for the inline-Router.
|
// path, with a fresh middleware stack for the inline-Router.
|
||||||
func (r *Router) Group(fn func(grp *Router)) {
|
func (r *Router) Group(fn func(grp *Router)) {
|
||||||
|
if r == nil {
|
||||||
|
panic("mux: Resource() called on nil")
|
||||||
|
}
|
||||||
|
|
||||||
if fn == nil {
|
if fn == nil {
|
||||||
return
|
panic("mux: Group() requires callback")
|
||||||
}
|
}
|
||||||
|
|
||||||
im := r.With()
|
im := r.With()
|
||||||
fn(im)
|
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)) {
|
func (r *Router) Resource(pattern string, fn func(resource *Resource)) {
|
||||||
if r == nil {
|
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 {
|
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{
|
fn(&Resource{
|
||||||
mux: r.mux,
|
mux: r.mux,
|
||||||
pattern: pattern,
|
pattern: pattern,
|
||||||
|
middlewares: mws,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user