middleware helmet changes.

router check and panic message change.
README enhancement
This commit is contained in:
2025-05-17 18:55:15 +05:30
parent e6a8880fd3
commit f8cdf3a511
5 changed files with 520 additions and 134 deletions

View File

@@ -30,55 +30,65 @@ func (r *Router) Use(h ...func(http.Handler) http.Handler) {
// GET method route
func (r *Router) GET(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, pattern, h)
r.handle(http.MethodGet, pattern, h)
}
// HEAD method route
func (r *Router) HEAD(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodHead, pattern, h)
r.handle(http.MethodHead, pattern, h)
}
// POST method route
func (r *Router) POST(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPost, pattern, h)
r.handle(http.MethodPost, pattern, h)
}
// PUT method route
func (r *Router) PUT(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPut, pattern, h)
r.handle(http.MethodPut, pattern, h)
}
// PATCH method route
func (r *Router) PATCH(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPatch, pattern, h)
r.handle(http.MethodPatch, pattern, h)
}
// DELETE method route
func (r *Router) DELETE(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodDelete, pattern, h)
r.handle(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
r.handle(http.MethodConnect, pattern, h)
}
// OPTIONS method route
func (r *Router) OPTIONS(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodOptions, pattern, h)
r.handle(http.MethodOptions, pattern, h)
}
// TRACE method route
func (r *Router) TRACE(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodTrace, pattern, h)
r.handle(http.MethodTrace, pattern, h)
}
// HandleFunc registers the handler function for the given pattern.
// handle registers the handler for the given pattern.
// If the given pattern conflicts, with one that is already registered, HandleFunc
// panics.
func (r *Router) handlerFunc(method, pattern string, h http.HandlerFunc) {
func (r *Router) handle(method, pattern string, h http.HandlerFunc) {
if r == nil {
panic("mux: func Handle() was called on nil")
}
if strings.TrimSpace(pattern) == "" {
panic("mux: pattern cannot be empty")
}
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
path := fmt.Sprintf("%s %s", method, pattern)
r.mux.Handle(path, stack(r.middlewares, h))
}
@@ -101,7 +111,7 @@ func (r *Router) With(middleware ...func(http.Handler) http.Handler) *Router {
// 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")
panic("mux: Group() called on nil")
}
if fn == nil {
@@ -143,7 +153,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.mux.ServeHTTP(w, req)
}
// TODO: proxy for aws lambda
// TODO: proxy for aws lambda and other serverless platforms
// stack middlewares(http handler) in order they are passed (FIFO)
func stack(middlewares []func(http.Handler) http.Handler, endpoint http.Handler) http.Handler {