httputils

package
v0.0.0-...-fd202e2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2021 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package httputils provides utilities to complement the net/http package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpRequest

func DumpRequest(w io.Writer, r *http.Request)

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

func DumpRequestText(w io.Writer, r *http.Request)

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

type SpaHandler struct {
	Fsys fs.FS
}

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

type SubHandler func(prefix string) (handler http.Handler)

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 NewSubMux

func NewSubMux(prefix string) *SubMux

NewSubMux constructs a SubMux with the supplied prefix.

func NewSubWithMux

func NewSubWithMux(prefix string, mux *http.ServeMux) *SubMux

NewSubWithMux constructs a SubMux with the supplied prefix, and existing ServceMux and re-using

func (*SubMux) Handle

func (sub *SubMux) Handle(pattern string, handler http.Handler)

Handle is a wrapper around http.ServeMux.Handle.

func (*SubMux) HandleFunc

func (sub *SubMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc is a wrapper around http.ServeMux.HandleFunc.

func (*SubMux) Handler

func (sub *SubMux) Handler(r *http.Request) (h http.Handler, pattern string)

Handler is a wrapper around http.ServeMux.Handler.

func (*SubMux) ServeHTTP

func (sub *SubMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a wrapper around http.ServeMux.ServeHTTP.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL