Documentation
¶
Overview ¶
Package httputils provides utilities to complement the net/http package.
Index ¶
- func DumpRequest(w io.Writer, r *http.Request)
- func DumpRequestText(w io.Writer, r *http.Request)
- func FileServerWithDefault(httpFs http.FileSystem, defaultContent DefaultFileContentFunc) http.Handler
- func MuxSubGroup(mux *http.ServeMux, prefix string, pattern string, constructor SubHandler)
- type DefaultFileContentFunc
- type SpaHandler
- type SubHandler
- type SubMux
- func (sub *SubMux) Handle(pattern string, handler http.Handler)
- func (sub *SubMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
- func (sub *SubMux) Handler(r *http.Request) (h http.Handler, pattern string)
- func (sub *SubMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DumpRequest ¶
DumpRequest provides a helper function to dump the received request to the output writer, e.g dump to standard-out... Synonym for function DumpRequestText()
httputils.DumpRequest(os.stdout, r)
func DumpRequestText ¶
DumpRequestText provides a helper function to dump the received request to the output writer in 'text' format, e.g dump to standard-out...
httputils.DumpRequestText(os.stdout, r)
func FileServerWithDefault ¶
func FileServerWithDefault(httpFs http.FileSystem, defaultContent DefaultFileContentFunc) http.Handler
FileServerWithDefault returns an http handler that serves the requested URL path from the given file-system. In the case that the requested URL path is not found within the given file-system, then the default content is provided via the given function.
func MuxSubGroup ¶
func MuxSubGroup(mux *http.ServeMux, prefix string, pattern string, constructor SubHandler)
MuxSubGroup adds a sub-group '/$pattern/' to ServeMux taking account of path prefix for '/$pattern' -> '$prefix/$pattern/' redirections. prefix: path to prepend for an absolute path (needed for redirection) pattern: pattern to match in ServeMux handler: http.Handler for pattern
Example usage...
// define a 'login' handler func loginHandler(prefix string) http.Handler { mux := http.NewServeMux() httputils.MuxSubGroup(mux, prefix, "/", httputils.SubHandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "login handler\n") })) httputils.MuxSubGroup(mux, prefix, "/check", httputils.SubHandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "check handler\n") })) return mux } // define a handler for 'auth' that registers the login handler under subpath '/login' func authHandler(prefix string) http.Handler { mux := http.NewServeMux() httputils.MuxSubGroup(mux, prefix, "/", httputils.SubHandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "auth handler\n") })) httputils.MuxSubGroup(mux, prefix, "/login", loginHandler) return mux } // define root handler that registers the auth handler under subpath '/auth' (which implies also '/auth/login') root := http.NewServeMux() httputils.MuxSubGroup(root, prefix, "/", httputils.SubHandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "root handler\n") })) httputils.MuxSubGroup(root, "", "/auth", authHandler)
Types ¶
type DefaultFileContentFunc ¶
type DefaultFileContentFunc func(httpFs http.FileSystem, w http.ResponseWriter, r *http.Request)
DefaultFileContentFunc is a function type an implementation of which provides the default content from the given file-system to the given http response writer.
type SpaHandler ¶
SpaHandler implements the http.Handler interface, so we can use it to respond to HTTP requests. The supplied filesystem is used to deliver the static content, which is assumed to be at the root of the filesystem.
func NewSpaHandler ¶
func NewSpaHandler(fsys fs.FS) *SpaHandler
NewSpaHandler creates an SpaHandler instance whose static content is provided at the root of the supplied filesystem.
func (SpaHandler) ServeHTTP ¶
func (h SpaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP inspects the URL path to locate a file within the filesystem on the SPA handler. If a file is found, it will be served. If not, the path is adjusted to serve the root path, i.e. index.html. This is suitable behavior for serving an SPA (single page application).
type SubHandler ¶
SubHandler type of a function that creates a Handler that optionally takes account of the provided path prefix.
func SubHandlerFunc ¶
func SubHandlerFunc(f http.HandlerFunc) SubHandler
SubHandlerFunc is a helper function to wrap a simple HandlerFunc as a SubHandler function.
type SubMux ¶
type SubMux struct {
// contains filtered or unexported fields
}
SubMux is a wrapper for http.ServeMux that supports a path prefix. This allows ServeMux instances to service subpaths. Example usage...
zzz
func NewSubWithMux ¶
NewSubWithMux constructs a SubMux with the supplied prefix, and existing ServceMux and re-using
func (*SubMux) HandleFunc ¶
HandleFunc is a wrapper around http.ServeMux.HandleFunc.