view

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package view provides an HTTP middleware to provide a simple View, based on Go templates.

Index

Constants

This section is empty.

Variables

View Source
var ErrorHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	err := ReqGetError(r)
	fmt.Fprintf(os.Stderr, "[ErrorHandler] %v\n", err)
	status := httperr.HTTPStatus(err)
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Header().Set("X-Content-Type-Options", "nosniff")
	w.WriteHeader(status)
	if _, err := fmt.Fprintf(w, "%d %s", status, http.StatusText(status)); err != nil {
		fmt.Fprintf(os.Stderr, "Failed to write HTTP response: %s\n", err)
	}
})

ErrorHandler is the default error handler. It logs the error to stderr, for debugging, and returns a generic error to the HTTP client, to prevent leaking of any sensitive information that may be contained in unvetted errors.

If err implements the StatusError interface, the returned status code will be sent to the client. Otherwise, status 500 (Internal Server Error) will be used.

Functions

func GetData

func GetData(ctx context.Context) map[string]interface{}

GetData returns the data structure which will be passed to the template for rendering.

func GetError

func GetError(ctx context.Context) error

GetError returns the template rendering error stored in the context.

func ReqGetData

func ReqGetData(r *http.Request) map[string]interface{}

ReqGetData returns the data structure which will be passed to the template for rendering.

func ReqGetError

func ReqGetError(r *http.Request) error

ReqGetError returns the template rendering error stored in the request's context.

Types

type Config

type Config struct {
	// RootPath, if provided, will be stripped from the beginning of
	// request URLs before traversing SearchPath.
	//
	// Any requests not under RootPath will return a 500 error.
	RootPath string

	// Include is a list of file globs to be loaded with each template.
	Include []string

	// SearchPath is a list of directories which are to be recursively traversed to
	// look for templates to parse.
	SearchPath []string

	// TemplateExtension is the extensions which will be considered for
	// parsing when traversing IncludePaths. If unset, all files will be
	// considered.
	TemplateExtension string

	// EntryPoint is the name of the template to execute instead of the
	// default. Typically, this would be a template loaded from the Helpers
	// list.
	EntryPoint string

	// ErrorHandler will receive any unhandled errors for processing. If
	// not defined, the default is used.
	ErrorHandler http.Handler

	// DirectoryIndex sets the list of resources to look for when a client
	// requests a directory. Do not include TemplateExtension in files in
	// this list.
	DirectoryIndex []string
}

Config are the configuration options for a Template.

type ErrorHandlerFunc

type ErrorHandlerFunc func(http.ResponseWriter, *http.Request) error

ErrorHandlerFunc extends a standard http.HandlerFunc

type Template

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

Template wraps html/template.Template

func New

func New(conf Config) (*Template, error)

New instantiates a new Template, based on Config.

func (*Template) Handle

func (t *Template) Handle(fn ErrorHandlerFunc) http.Handler

Handle wraps an ErrorHandlerFunc, returning a standard http.Handler.

func (*Template) HandleFunc

func (t *Template) HandleFunc(fn ErrorHandlerFunc) http.HandlerFunc

HandleFunc wraps an ErrorHandlerFunc, returning a standard http.HandlerFunc

func (*Template) Middleware

func (t *Template) Middleware(next http.Handler) http.Handler

Middleware allows you to use a Template instance as middleware, passing control on to other standard http handlers (or middleware).

When using this method, the view will only be rendered if next doesn't write to w. If next does write to n, this method acts essentially as a pass-through.

func (*Template) NotFoundHandler

func (t *Template) NotFoundHandler(w http.ResponseWriter, r *http.Request)

NotFoundHandler is an http.HandlerFunc which can be passed to your router to handle not-found errors. It calls RenderError{} with httperr.StandardError(http.StatusNotFound) as the error.

func (*Template) Render

func (t *Template) Render(w http.ResponseWriter, r *http.Request)

Render renders the template for r.

func (*Template) RenderError

func (t *Template) RenderError(w http.ResponseWriter, r *http.Request, err error)

RenderError is called to render any errors.

Jump to

Keyboard shortcuts

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