-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
Proposal Details
With the HTTP router changes in go1.22, many developers are considering standard library for daily use or production. Although, as of now, NotFound and MethodNotAllowed handlers are hard coded, with no way of change or overriding them.
If we look at thefindHandler
function, we see:
//...
allowedMethods := mux.matchingMethods(host, path)
if len(allowedMethods) > 0 {
return HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header().Set("Allow", strings.Join(allowedMethods, ", "))
Error(w, StatusText(StatusMethodNotAllowed), StatusMethodNotAllowed)
}), "", nil, nil
}
return NotFoundHandler(), "", nil, nil
//...
I propose adding the option to set these two handlers.
Implementation
Referring to my PR (#67970), a new MuxOption
type is introduced and two functions called WithNotFoundHandler
and WithNotAllowedHandler
are added.
Also, function NewServeMux
is updated to accept variadic parameter of type MuxOption
. This ensures backward compatibility, while allowing for customization.
To make sure the default value of ServeMux
is valid as well, findHandler
function is updated to use custom handlers if provided, or to use the default handlers otherwise.