Last active
December 22, 2023 22:01
-
-
Save pkieltyka/d4ab2e385febcc195a9cea4ef8e61227 to your computer and use it in GitHub Desktop.
wrapped chi.Router with basics for acl functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aclmux | |
import ( | |
"net/http" | |
"regexp" | |
"strings" | |
"github.com/go-chi/chi" | |
) | |
type Mux struct { | |
Mux *chi.Mux | |
} | |
type Router interface { | |
chi.Router | |
} | |
var _ Router = &Mux{} | |
func NewRouter(route string) *Mux { | |
r := &Mux{chi.NewRouter()} | |
if route != "" { | |
r.Mux.Use(Route(route)) | |
} | |
return r | |
} | |
func (r *Mux) Use(middlewares ...func(http.Handler) http.Handler) { | |
r.Mux.Use(middlewares...) | |
} | |
func (r *Mux) Handle(pattern string, handler http.Handler) { | |
r.with(Route(pattern), ResourceControl).Handle(pattern, handler) | |
} | |
func (r *Mux) HandleFunc(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).HandleFunc(pattern, handler) | |
} | |
func (r *Mux) Method(method, pattern string, handler http.Handler) { | |
r.with().Method(method, pattern, handler) | |
} | |
func (r *Mux) MethodFunc(method, pattern string, handler http.HandlerFunc) { | |
r.with().MethodFunc(method, pattern, handler) | |
} | |
func (r *Mux) Connect(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Connect(pattern, handler) | |
} | |
func (r *Mux) Head(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Head(pattern, handler) | |
} | |
func (r *Mux) Get(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Get(pattern, handler) | |
} | |
func (r *Mux) Post(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Post(pattern, handler) | |
} | |
func (r *Mux) Put(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Put(pattern, handler) | |
} | |
func (r *Mux) Patch(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Patch(pattern, handler) | |
} | |
func (r *Mux) Delete(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Delete(pattern, handler) | |
} | |
func (r *Mux) Trace(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Trace(pattern, handler) | |
} | |
func (r *Mux) Options(pattern string, handler http.HandlerFunc) { | |
r.with(Route(pattern), ResourceControl).Options(pattern, handler) | |
} | |
func (r *Mux) NotFound(handlerFn http.HandlerFunc) { | |
r.Mux.NotFound(handlerFn) | |
} | |
func (r *Mux) MethodNotAllowed(handlerFn http.HandlerFunc) { | |
r.Mux.MethodNotAllowed(handlerFn) | |
} | |
func (r *Mux) ServeHTTP(w http.ResponseWriter, req *http.Request) { | |
r.Mux.ServeHTTP(w, req) | |
} | |
func (r *Mux) with(middlewares ...func(http.Handler) http.Handler) chi.Router { | |
return r.Mux.With(middlewares...).(*chi.Mux) | |
} | |
func (r *Mux) With(middlewares ...func(http.Handler) http.Handler) chi.Router { | |
mx := r.with(middlewares...).(*chi.Mux) | |
return &Mux{mx} | |
} | |
// NOTE: the following method will add a ResourceControl() for anytime a route is | |
// used with Group(), but in fact, we may not always want this.. it requires | |
// turning on and going through the API to ensure the acl logic and flow makes sense. | |
func (r *Mux) Group(fn func(r chi.Router)) chi.Router { | |
mx := r.Mux.With().(*chi.Mux) | |
im := &Mux{mx} | |
if fn != nil { | |
fn(im) | |
} | |
return im | |
} | |
func (r *Mux) Route(pattern string, fn func(r chi.Router)) chi.Router { | |
subRouter := NewRouter("") | |
if fn != nil { | |
fn(subRouter) | |
} | |
r.Mount(pattern, subRouter) | |
return subRouter | |
} | |
func (r *Mux) Mount(pattern string, handler http.Handler) { | |
r.Mux.Mount(pattern, handler) | |
} | |
func (r *Mux) Middlewares() chi.Middlewares { | |
return r.Mux.Middlewares() | |
} | |
func (r *Mux) Routes() []chi.Route { | |
return r.Mux.Routes() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment