handlers: github.com/olihawkins/handlers Index | Files

package handlers

import "github.com/olihawkins/handlers"

Package handlers is a small library of utility http handlers that are useful for building web applications. It includes a NotFoundHandler, an ErrorHandler, and a FileHandler.

The NotFoundHandler and ErrorHandler provide a simple way to respond to the client with custom 404 and 500 status pages: create your own application specific error page templates and call each of the handler's Serve methods with the appropriate arguments.

These two handlers are intended to be used indirectly, from inside other handlers where server errors or page not found errors occur and you need to report them to the browser. However, both of these types implement the http.Handler interface with a ServeHTTP method, which shows their default behaviour when bound to a specific route.

The FileHandler provides similar functionality to the FileServer in the net/http package, but with two differences: it will not show directory listings for directories under its path, and it will respond to any request for a non-existent file with the given NotFoundHandler.

Index

Package Files

handlers.go

type ErrorHandler Uses

type ErrorHandler struct {
    // contains filtered or unexported fields
}

ErrorHandler serves error messages with the given template. The template can access the message served by the handler with the {{.ErrorMessage}} tag.

func LoadErrorHandler Uses

func LoadErrorHandler(templatePath string, defaultMessage string, displayErrors bool) *ErrorHandler

LoadErrorHandler is a convenience function that returns a new ErrorHandler using the template file specified by templatePath. The function first loads the template and then creates the ErrorHandler using NewErrorHandler.

func NewErrorHandler Uses

func NewErrorHandler(template *template.Template, defaultMessage string, displayErrors bool) *ErrorHandler

NewErrorHandler returns a new ErrorHandler with the handler values initialised. The handler uses the given template to print an error message. The template must display {{.ErrorMessage}}. The default error message is set to message. The display argument controls whether error messages passed to the handler's ServeError function are shown to the user on the error page, or whether the default error message is shown instead. This allows detailed error messages to be printed to the screen during development, but turned off in production. The handler's AlwaysServeError method forces the display of a particular error message even if displayErrors is set to false.

func (*ErrorHandler) AlwaysServeError Uses

func (h *ErrorHandler) AlwaysServeError(w http.ResponseWriter, message string)

AlwaysServeError serves the given error message in the error template. This method overrides the default error message, irrespective of whether displayErrors is false, and ensures that the given message is always shown.

func (*ErrorHandler) ServeError Uses

func (h *ErrorHandler) ServeError(w http.ResponseWriter, message string)

ServeError serves the appropriate error message in the error template depending on the value of displayErrors. If displayErrors is true then the given message is shown, otherwise the default error message is shown.

func (*ErrorHandler) ServeHTTP Uses

func (h *ErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the default error message in the error template.

type ErrorMessage Uses

type ErrorMessage struct {
    ErrorMessage string
}

ErrorMessage holds the message passed to the error template. The template can access the message field with the {{.ErrorMessage}} tag.

type FileHandler Uses

type FileHandler struct {
    // contains filtered or unexported fields
}

FileHandler serves files requested under the given url path from the given directory. The url path should be the same as the path to which the handler is bound with http.Handle. If the file is not found the handler serves a 404 using the given notFoundHandler. The notFoundHandler can be any Handler, but its ServeHTTP method should return a 404. Unlike Go's built-in FileServer, FileHandler will not return directory listings for directories without an index.html and will instead respond with a 404.

func NewFileHandler Uses

func NewFileHandler(urlPath string, directory string, notFoundHandler http.Handler) *FileHandler

NewFileHandler returns a new FileHandler with the handler values initialised.

func (*FileHandler) ServeHTTP Uses

func (h *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a wrapper around http.ServeFile, with paths and response values modified to provide the appropriate behaviour for the FileHandler.

type NotFoundData Uses

type NotFoundData struct {
    Path string
}

NotFoundData holds the path passed to the handler's template. The template can access the message field with the {{.Path}} tag.

type NotFoundHandler Uses

type NotFoundHandler struct {
    // contains filtered or unexported fields
}

NotFoundHandler serves a 404 with the given template. The template can access the path to the file not found with {{.Path}} tag.

func LoadNotFoundHandler Uses

func LoadNotFoundHandler(templatePath string) *NotFoundHandler

LoadNotFoundHandler is a convenience function that returns a new NotFoundHandler using the template file specified by templatePath. The function first loads the template and then creates the NotFoundHandler using NewNotFoundHandler.

func NewNotFoundHandler Uses

func NewNotFoundHandler(template *template.Template) *NotFoundHandler

NewNotFoundHandler returns a new NotFoundHandler with the handler values initialised. The handler uses the given template to print the path to the file not found with a 404. The template must display {{.Path}}.

func (*NotFoundHandler) ServeHTTP Uses

func (h *NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the path in the handler's template.

Package handlers imports 7 packages (graph) and is imported by 1 packages. Updated 2016-09-28. Refresh now. Tools for package owners.