views

package
v1.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: GPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrMissingArg errs.Error = "missing url argument or argument is empty"
)

Variables

This section is empty.

Functions

func Cache added in v1.6.6

func Cache(view http.Handler, duration cache.Duration, cacheBackends ...string) http.Handler

Cache caches the response of a view for a given duration.

func Invoke

func Invoke(view View, w http.ResponseWriter, req *http.Request, allowedMethods ...string) error

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:

  1. View - A generic view that can handle any type of request.
  2. MethodsView - A view that declares acceptable methods.
  3. ContextGetter - A view that can initiate a context for the request.
  4. TemplateGetter - A view that can get the template path of the template to render.
  5. TemplateView - A view that can render a template from a template path with a context.
  6. ErrorHandler - A view that can handle errors.
  7. Renderer - A view that can render directly to the response writer with a context.
  8. TemplateKeyer - A view that can get the base key for the template. This is useful for rendering a sub-template with a base template.
  9. Checker - A view that can check the request before serving it. This is useful for checking if the request is valid before serving it.

func Serve

func Serve(view View) http.Handler

Serve serves a view.

This function is a generic view handler that can be used to serve any type of view which the `Invoke` function can handle.

Types

type BaseView

type BaseView struct {
	AllowedMethods  []string
	BaseTemplateKey string
	TemplateName    string
	GetContextFn    func(req *http.Request) (ctx.Context, error)
}

func (*BaseView) GetContext

func (v *BaseView) GetContext(req *http.Request) (ctx.Context, error)

func (*BaseView) GetTemplate

func (v *BaseView) GetTemplate(req *http.Request) string

func (*BaseView) Methods

func (v *BaseView) Methods() []string

func (*BaseView) Render

func (v *BaseView) Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) error

func (*BaseView) ServeXXX

func (v *BaseView) ServeXXX(w http.ResponseWriter, req *http.Request)

Placeholder method, will never get called.

type CachedResponse added in v1.6.6

type CachedResponse struct {
	ResponseStatusCode int         `json:"status_code"`
	ResponseHeader     http.Header `json:"header"`
	ResponseBody       byteArray   `json:"body"`
}

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 ContextGetter interface {
	View
	GetContext(req *http.Request) (ctx.Context, error)
}

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 (d *DetailView[T]) GetContext(req *http.Request) (ctx.Context, error)

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

type ErrorFunc

type ErrorFunc func(w http.ResponseWriter, req *http.Request, err error, code int)

type ErrorHandler

type ErrorHandler interface {
	// HandleError handles the error.
	HandleError(w http.ResponseWriter, req *http.Request, err error, code int)
}

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]) Check

func (v *FormView[T]) Check(w http.ResponseWriter, req *http.Request) error

func (*FormView[T]) Fail

func (v *FormView[T]) Fail(w http.ResponseWriter, req *http.Request, err error)

func (*FormView[T]) FormFromCtx

func (v *FormView[T]) FormFromCtx(context ctx.Context) (t T)

func (*FormView[T]) GetContext

func (v *FormView[T]) GetContext(req *http.Request) (ctx.Context, error)

func (*FormView[T]) GetForm

func (v *FormView[T]) GetForm(req *http.Request) T

func (*FormView[T]) Render

func (v *FormView[T]) Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) error

type MethodsView

type MethodsView interface {
	View
	Methods() []string
}

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 TemplateGetter interface {
	// GetTemplate returns the template that will be rendered.
	GetTemplate(req *http.Request) string
}

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)
}

Directories

Path Synopsis
templ: version: v0.2.707
templ: version: v0.2.707

Jump to

Keyboard shortcuts

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