Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var FormDecoder = schema.NewDecoder() // Exposed to allow users to modify behaviour.
FormDecoder is the schema.Decoder used for every FormHandler.
Functions ¶
func DefaultEmptyHandler ¶
func DefaultEmptyHandler(w http.ResponseWriter, _ *http.Request)
DefaultEmptyHandler simply responds with a 404.
func DefaultErrorHandler ¶
DefaultErrorHandler writes the content of the error as the response and responds with a 500.
func DefaultRedirectHandler ¶ added in v1.0.0
func DefaultRedirectHandler(w http.ResponseWriter, r *http.Request)
DefaultRedirectHandler should redirect back to previous page.
Types ¶
type FormHandler ¶ added in v1.0.0
type FormHandler[T any] struct { // Handler is the main handler of a FormHandler. // The original http.Request and deserialized form are given as parameters. Handler func(*http.Request, *T) error // ErrorHandler is used whenever Handler returns an error. // The default value is DefaultErrorHandler. ErrorHandler func(error) http.Handler // RedirectHandler is invoked once Handler succeeds, and is meant to provide functionality for redirecting // after a successful POST request. // The default value is DefaultRedirectHandler. RedirectHandler http.Handler }
FormHandler is a http.Handler compatible struct which handles http.MethodPost requests with form bodies.
func NewFormHandler ¶ added in v1.0.0
func NewFormHandler[T any](handler func(*http.Request, *T) error, options ...FormHandlerOption[T]) *FormHandler[T]
NewFormHandler initializes a new FormHandler, and only the FormHandler.Handler is required. All other fields have default values.
func (FormHandler[T]) ServeHTTP ¶ added in v1.0.0
func (f FormHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP delegates all logic to the handler methods of FormHandler.
type FormHandlerOption ¶ added in v1.0.0
type FormHandlerOption[T any] func(p *FormHandler[T])
type PageHandler ¶
type PageHandler[T any] struct { // Handler determines what data is to be filled in Template. Handler func(*http.Request) (*T, error) // Template is the HTML template for our page. // The fields of this Template are filled by the result of Handler. Template *template.Template // ErrorHandler is used whenever Handler returns an error. // The default value is DefaultErrorHandler. ErrorHandler func(error) http.Handler // EmptyHandler is used whenever Handler returns a nil. // The default value is DefaultEmptyHandler. EmptyHandler http.Handler }
PageHandler represent the content to be served as the result of some logic being performed in the MainPageHandler. The template can be seen a structure for this data.
func NewPageHandler ¶
func NewPageHandler[T any](handler func(*http.Request) (*T, error), template *template.Template, options ...PageHandlerOption[T]) *PageHandler[T]
NewPageHandler requires only PageHandler.Handler and PageHandler.Template fields be provided. Everything else has a default.
func (*PageHandler[T]) ServeHTTP ¶
func (p *PageHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP delegates all logic to the handler methods of PageHandler.
type PageHandlerOption ¶
type PageHandlerOption[T any] func(p *PageHandler[T])