Documentation ¶
Index ¶
- Constants
- func Cache(view http.Handler, duration cache.Duration, cacheBackends ...string) http.Handler
- func Invoke(view View, w http.ResponseWriter, req *http.Request, allowedMethods ...string) error
- func Serve(view View) http.Handler
- type BaseView
- func (v *BaseView) GetContext(req *http.Request) (ctx.Context, error)
- func (v *BaseView) GetTemplate(req *http.Request) string
- func (v *BaseView) Methods() []string
- func (v *BaseView) Render(w http.ResponseWriter, req *http.Request, templateName string, ...) error
- func (v *BaseView) ServeXXX(w http.ResponseWriter, req *http.Request)
- type CachedResponse
- type Checker
- type ContextGetter
- type ControlledView
- type DetailView
- func (d *DetailView[T]) GetContext(req *http.Request) (ctx.Context, error)
- func (d *DetailView[T]) GetObject(req *http.Request, urlArg string) (v T, err error)
- func (d *DetailView[T]) GetURLArg(req *http.Request) (string, error)
- func (d *DetailView[T]) Setup(w http.ResponseWriter, req *http.Request) (http.ResponseWriter, *http.Request)
- type ErrorFunc
- type ErrorHandler
- type FormView
- func (v *FormView[T]) Check(w http.ResponseWriter, req *http.Request) error
- func (v *FormView[T]) Fail(w http.ResponseWriter, req *http.Request, err error)
- func (v *FormView[T]) FormFromCtx(context ctx.Context) (t T)
- func (v *FormView[T]) GetContext(req *http.Request) (ctx.Context, error)
- func (v *FormView[T]) GetForm(req *http.Request) T
- func (v *FormView[T]) Render(w http.ResponseWriter, req *http.Request, templateName string, ...) error
- type MethodsView
- type Renderer
- type SetupView
- type TemplateGetter
- type TemplateKeyer
- type TemplateView
- type View
Constants ¶
View Source
const (
ErrMissingArg errs.Error = "missing url argument or argument is empty"
)
Variables ¶
This section is empty.
Functions ¶
func Invoke ¶
Invoke invokes the view and appropriately handles the request.
This view is a generic view that can be used to render templates.
It is moved into a separate function to better handle views and their composition.
Example:
type MyView struct { TemplateView // Some sort of base view with a get context method. // Fields here... } func (v *MyView) GetContext(req *http.Request) (ctx.Context, error) { // Get the context here... }
In regular GO, the above TemplateView would not be able to get the context of the 'MyView' struct.
This means the overridden GetContext method would remain completely invisible and thus go unused.
This prevents a whole lot of flexibility in the design of the views.
This function is a workaround to allow for a more class-based approach.
It can handle the following types of views:
- View - A generic view that can handle any type of request.
- MethodsView - A view that declares acceptable methods.
- ContextGetter - A view that can initiate a context for the request.
- TemplateGetter - A view that can get the template path of the template to render.
- TemplateView - A view that can render a template from a template path with a context.
- ErrorHandler - A view that can handle errors.
- Renderer - A view that can render directly to the response writer with a context.
- TemplateKeyer - A view that can get the base key for the template. This is useful for rendering a sub-template with a base template.
- Checker - A view that can check the request before serving it. This is useful for checking if the request is valid before serving it.
Types ¶
type BaseView ¶
type BaseView struct { AllowedMethods []string BaseTemplateKey string TemplateName string GetContextFn func(req *http.Request) (ctx.Context, error) }
type CachedResponse ¶ added in v1.6.6
type Checker ¶
type Checker interface { View // Check checks the request before serving it. // Useful for checking if the request is valid before serving it. // Like checking permissions, etc... Check(w http.ResponseWriter, req *http.Request) error // Fail is a helper function to fail the request. // This can be used to redirect, etc. Fail(w http.ResponseWriter, req *http.Request, err error) }
type ContextGetter ¶
type ControlledView ¶
type ControlledView interface {
TakeControl(w http.ResponseWriter, req *http.Request)
}
type DetailView ¶
type DetailView[T any] struct { BaseView ContextName string URLArgName string GetObjectFn func(req *http.Request, urlArg string) (T, error) }
func (*DetailView[T]) GetContext ¶
func (*DetailView[T]) GetObject ¶
func (d *DetailView[T]) GetObject(req *http.Request, urlArg string) (v T, err error)
func (*DetailView[T]) GetURLArg ¶
func (d *DetailView[T]) GetURLArg(req *http.Request) (string, error)
func (*DetailView[T]) Setup ¶
func (d *DetailView[T]) Setup(w http.ResponseWriter, req *http.Request) (http.ResponseWriter, *http.Request)
type ErrorHandler ¶
type FormView ¶
type FormView[T forms.Form] struct { BaseView GetFormFn func(req *http.Request) T GetInitialFn func(req *http.Request) map[string]interface{} ValidFn func(req *http.Request, form T) error InvalidFn func(req *http.Request, form T) error SuccessFn func(w http.ResponseWriter, req *http.Request, form T) CheckPermissions func(w http.ResponseWriter, req *http.Request) error FailsPermissions func(w http.ResponseWriter, req *http.Request, err error) }
func (*FormView[T]) FormFromCtx ¶
func (*FormView[T]) GetContext ¶
type MethodsView ¶
type Renderer ¶
type Renderer interface { View ContextGetter // Render renders the template with the given context. Render(w http.ResponseWriter, req *http.Request, context ctx.Context) error }
type SetupView ¶
type SetupView interface { View Setup(w http.ResponseWriter, req *http.Request) (http.ResponseWriter, *http.Request) }
type TemplateGetter ¶
type TemplateKeyer ¶
type TemplateKeyer interface { // GetBaseKey returns the base key for the template. GetBaseKey() string }
type TemplateView ¶
type TemplateView interface { View ContextGetter TemplateGetter // Render renders the template with the given context. Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) error }
type View ¶
type View interface { // ServeXXX is a method that will never get called. // It is a placeholder for the actual method that will be called. // The actual method will be determined by the type of the request. // For example, if the request is a GET request, then ServeGET will be called. // If the request is a POST request, then ServePOST will be called. // etc... ServeXXX(w http.ResponseWriter, req *http.Request) }
Click to show internal directories.
Click to hide internal directories.